When I access to database in AsyncTask, my app crash and the reason is database is locked


#1

My app crashed immediately after it’s started. And database no operation before the crash. So I’ve no idea why “database is locked”? And who locked the database? Isn’t sqlcipher thread-safe ?

The crash log is as follows:

E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-10734
net.sqlcipher.database.SQLiteException: database is locked: BEGIN EXCLUSIVE;
at net.sqlcipher.database.SQLiteDatabase.native_execSQL(Native Method)
at net.sqlcipher.database.SQLiteDatabase.execSQL(SQLiteDatabase.java:1831)
at net.sqlcipher.database.SQLiteDatabase.beginTransactionWithListener(SQLiteDatabase.java:584)
at net.sqlcipher.database.SQLiteDatabase.beginTransaction(SQLiteDatabase.java:538)
at com.myapp.dao.DepartMentDao.insertDepart(DepartMentDao.java:51)
at com.myapp.service.MOAServiceImpl.parseData(MOAServiceImpl.java:750)
at com.myapp.service.MOAServiceImpl.access$000(MOAServiceImpl.java:141)
at com.myapp.service.MOAServiceImpl$1.run(MOAServiceImpl.java:722)
at java.lang.Thread.run(Thread.java:864)
………………………………

12-10 12:37:31.927 29030-29073/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: net.sqlcipher.database.SQLiteException: database is locked
at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2098)
at net.sqlcipher.database.SQLiteDatabase.(SQLiteDatabase.java:1964)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:881)
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:913)
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:99)
at com.myapp.util.DatabaseUtils.getDatabase(DatabaseUtils.java:5055)


#2

SQLCipher for Android is compiled with -DSQLITE_THREADSAFE, however while the wrapping Java library contains some threading primitives for certain operations, is not entirely thread safe. It is recommended that you include a synchronization mechanism to allow for coordinated access to your connection.

It is difficult to say why your application crashed without addition source information. Are you able to debug through it? Does the crash always occur?