如何在方向更改上管理SQLite连接

时间:2017-09-21 14:04:36

标签: android multithreading android-sqlite

我正在多个异步调用中读/写我的SQLite数据库,但是尝试按照建议here这样做。这一切在正常操作中都能正常工作。但是,如果我将方向翻转为横向和背面,则在我的getWritableDatabase中尝试onCreate时会出现以下错误。

09-21 06:52:49.835 E: (5) database is locked
09-21 06:52:49.840 E: Failed to open database '/data/user/0/com.pipperpublishing.soccerrefpro/databases/Games.db'.
                      android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
                      #################################################################
                      Error Code : 5 (SQLITE_BUSY)
                      Caused By : The database file is locked.
                        (database is locked (code 5): , while compiling: PRAGMA journal_mode)
                      #################################################################
                          at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                          at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1005)
                          at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:709)
                          at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:386)
                          at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:360)
                          at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:252)
                          at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:203)
                          at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:514)
                          at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
                          at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
                          at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1063)
                          at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1018)
                          at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:824)
                          at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:681)
                          at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                          at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                          at com.pipperpublishing.refwatch.RefWatch.onCreate(RefWatch.java:548)
                          at android.app.Activity.performCreate(Activity.java:6912)
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
                          at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4974)
                          at android.app.ActivityThread.-wrap21(ActivityThread.java)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6688)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

据推测,我的一些异步线程仍然保持着数据库连接。我尝试在重新打开之前关闭数据库,但这没有用。

异步调用完成并不重要(实际上我抓住了导致的异常),但我想避免应用程序崩溃。

如果没有干净的数据库管理,我可以在onStop()中杀死所有剩余的异步线程。

建议欢迎。

0 个答案:

没有答案