I have a linked service that works with multiple threads that read and write to a SQLite database. I create an instance of my database assistant and get a database connection in the onCreate of my service, which should be executed in the main thread. Since this is a related service, there should always be only one service instance in memory. However, I still get what sometimes happens when a service tries to open a database or when one of the threads tries to execute a statement.
Service Code:
@Override public void onCreate() { super.onCreate(); MyDatabaseHelper helper = new MyDatabaseHelper(this); database = helper.getWritableDatabase(); }
Exceptions:
java.lang.RuntimeException: Unable to create service uk.co.example.service.MyService: android.database.sqlite.SQLiteException: unable to open database file at android.app.ActivityThread.handleCreateService(ActivityThread.java:1959) at android.app.ActivityThread.access$2500(ActivityThread.java:117) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3691) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.sqlite.SQLiteException: unable to open database file at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1960) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:887) at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:965) at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:958) at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:585) at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118) at uk.co.example.service.MyService.onCreate(MyService.java:69) at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949) ... 10 more android.database.sqlite.SQLiteException: error code 5: database is locked at android.database.sqlite.SQLiteStatement.native_execute(Native Method) at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61) at uk.co.example.aaaa(MyTable.java:147) at uk.co.example.service.bamrun(MySaveTask.java:84) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) at java.lang.Thread.run(Thread.java:1019)
Any ideas what could be causing this?
thanks
David
source share