Database locked exception

后端 未结 2 2114
囚心锁ツ
囚心锁ツ 2020-12-20 05:20

I have created a database in my application with 5 tables. my database is being updated from different threads. When i see the log i can see that there are database locked e

2条回答
  •  醉话见心
    2020-12-20 05:29

    The problem is that you use several database connections to your database. Thus, several threads try to update your table simultaneously and all these threads have different connections to your database.

    To avoid this problem in all your threads you need to use the same connection to the database, i.e. all your threads should use the same connection to the database (that is represented by SQLiteDabase object).

    Moreover, so as there is a file block on a sqlite file you'll not improve the performance of database upgrade using several threads (it's better to use only one thread to work with database). If you want to use several threads, you should use the same connection to the database and in this case Android will manage locks.

    The discussion of this problem you can find here: http://touchlabblog.tumblr.com/post/24474398246/android-sqlite-locking and here: What are the best practices for SQLite on Android?

提交回复
热议问题