About android Sqlite safety in multi-process case

喜夏-厌秋 提交于 2021-01-27 04:09:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!