質問

I have a service running that contains an SQLite DB and a Content Observer registered to the DB's URI. The Content Observer uses an SQLiteOpenHelper to query the data from the DB. So when new data is put into the services DB from other application via Content Provider the changed cursor will be observed and query the data to process. The problem is if I insert data at a fast pace from other applications I end up with both the Content Observer and Content Provider opening the DB and causes an SQLite error with a database lock.

I am using the getReadable and getWritable methods and closing the DB and any cursors after they are used. It just seem that the CP and CO are not in sync. How can I fix this problem?

UPDATE When I test this on a Motorola Atrix I have no problems. When I test it on a Samsung Galaxy Tab I get problems. On the Atrix it appears from my debug tags in the catlog that both the CO and the CP have the DB open at the same time... but still not problem like there is with the Galaxy. The Galaxy gives me this error:

11-04 12:12:13.490: ERROR/SqliteDatabaseCpp(19978): sqlite3_open_v2("/data/data/my.package/databases/raw_data_buffer.db", &handle, 6, NULL) failed
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978): Failed to open the database. closing it.
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:750)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at my.package.RawDataBufferDatabaseHelper.insert(RawDataBufferDatabaseHelper.java:93)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at my.package.NetService$NetworkPollingRunnable.run(NetService.java:166)
11-04 12:12:13.490: ERROR/SQLiteDatabase(19978):     at java.lang.Thread.run(Thread.java:1020)
11-04 12:12:13.490: WARN/dalvikvm(19978): threadid=9: thread exiting with uncaught exception (group=0x40189760)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978): FATAL EXCEPTION: Thread-14
11-04 12:12:13.490: ERROR/AndroidRuntime(19978): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:750)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
11-04 12:12:13.490: ERROR/AndroidRuntime(19978):     at my.package.RawDataBufferDatabaseHelper.insert(RawDataBufferDatabaseHelper.java:93)

役に立ちましたか?

解決

Closing the cursors really doesn't have any affect on anything in this case. Don't have your CO's directly accessing the database. Make every piece of your code go through the ContentProvider and in your content provider only have one writeable and one readable connection open (make these instance variables). That should keep everything consistent because then you'll only have one db connection that you're going through. As long as you're using the same db connection you're fine. The sqlite methods are reentrant.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top