问题
In my application, there exist more than one process, and in each process, I need access the same SQLite database (of course, it means more than 2 theads), so I worried about not only the thread-safety about the SQLite, but also the process-safety.
One solution for this case is using content-provider. But from android sdk, it warns that its methods may be called from multiple threads and therefore must be thread-safe. If content provider itself not necessarily means thread-safe, how can I assume it is process-safe?
The article also clarifies that SQLiteDatabase itself is synchronized by default, thus guaranteeing that no two threads will ever touch it at the same time. What if in the multi-process case? Can two processes modify the same table at the same time? Will it crash? I does not get any answer after googling....
回答1:
Multiple processes behave just like multiple threads, i.e., their transactions are safe from being interfered with by each other.
回答2:
You can find answer here: https://www.sqlite.org/faq.html (point 5). Briefly:
Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.
回答3:
If you use Android Room
, see https://issuetracker.google.com/issues/62334005
来源:https://stackoverflow.com/questions/25757344/about-android-sqlite-safety-in-multi-process-case