简体   繁体   English

Android SQLite数据库已被锁定

[英]Android SQLite database is locked

I have a bound service running multiple threads that read and write to an SQLite database. 我有一个绑定服务运行多个线程,读取和写入SQLite数据库。 I create an instance of my database helper and get a connection to the database in the onCreate of my service, which should execute on the main thread. 我创建了一个数据库助手的实例,并在我的服务的onCreate中获得了与数据库的连接,该连接应该在主线程上执行。 As it's a bound service there should only ever be one instance of the service in memory. 因为它是绑定服务,所以应该只在内存中存在一个服务实例。 However i still get exceptions occasionally when the service tries to open the 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.a.a.a.a(MyTable.java:147)
at uk.co.example.service.b.a.m.run(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 大卫

Sqlite now supports a new journal mode called Writing-Ahead Logging, which is suitable for concurrent access to sqlite. Sqlite现在支持一种名为Writing-Ahead Logging的新日志模式,它适用于对sqlite的并发访问。 you may want to take a look at http://www.sqlite.org/wal.html . 您可以查看http://www.sqlite.org/wal.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM