I am trying to open a SQLiteDatabase in a "Service" which fails with the following exception:
10-21 19:33:23.547: E/SqliteDatabaseCpp(31208): sqlite3_exec - Failed to set synchronous mode = 1(Normal)
10-21 19:33:23.547: E/SQLiteDatabase(31208): Failed to open the database. closing it.
10-21 19:33:23.547: E/SQLiteDatabase(31208): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1135)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1086)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1173)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:858)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
10-21 19:33:23.547: E/SQLiteDatabase(31208): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
Can somebody please explain what this means? and how can it be fixed?. The database is opened in the MainActivity and a Service.
I think it is an access conflict happened when the db was used (locked) by one thread (the activity?), the other thread (the service?) tried to use (lock) it but failed.
You can make your own SQLiteOpenHelper a singleton class, and then share it between the activity and service, and then the class will handle the conflict properly for you. See the discussion in this thread
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With