6

这段代码:

public void foo() {
    new Thread() {
        @Override
        public void run() {
            ShaufelDatabaseHelper dbHelper = ShaufelDatabaseHelper.getInstance(parentActivity);
            SQLiteDatabase connection = null;
            Cursor queryData = null;

            final String[] values = new String[8];

            try{
                connection = dbHelper.getReadableDatabase();

                queryData = connection.rawQuery(         // line 132
                    "SELECT name, firstname, section, row, gravenr, type, workorder, comment "+
                    "FROM orders, graves "+
                    "WHERE orders.id=? AND graves.id=orders.id"
                    , new String[] {orderID});

                queryData.moveToFirst(); // line 138

                // ...
        } catch(Throwable t) {
            // ...
        }
    }.start();
}

给了我这些例外:

10-29 15:19:55.103: E/AndroidRuntime(23102): FATAL EXCEPTION: Thread-1387
10-29 15:19:55.103: E/AndroidRuntime(23102): java.lang.IllegalMonitorStateException
10-29 15:19:55.103: E/AndroidRuntime(23102):    at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:126)
10-29 15:19:55.103: E/AndroidRuntime(23102):    at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1239)
10-29 15:19:55.103: E/AndroidRuntime(23102):    at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:431)
10-29 15:19:55.103: E/AndroidRuntime(23102):    at android.database.sqlite.SQLiteDatabase.unlock(SQLiteDatabase.java:487)
10-29 15:19:55.103: E/AndroidRuntime(23102):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:62)
10-29 15:19:55.103: E/AndroidRuntime(23102):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
10-29 15:19:55.103: E/AndroidRuntime(23102):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
10-29 15:19:55.103: E/AndroidRuntime(23102):    at de.l_one.app.shaufel.OrderDetailView$3.run(OrderDetailView.java:132)

10-29 15:21:07.953: E/SQLiteQuery(23133): exception: library routine called out of sequence; query: SELECT name, firstname, section, row, gravenr, type, workorder, comment FROM orders, graves WHERE orders.id=? AND graves.id=orders.id
10-29 15:21:07.953: E/SQL order detail(23133): android.database.sqlite.SQLiteMisuseException: library routine called out of sequence
10-29 15:21:07.953: E/SQL order detail(23133): android.database.sqlite.SQLiteMisuseException: library routine called out of sequence
10-29 15:21:07.953: E/SQL order detail(23133):  at android.database.sqlite.SQLiteQuery.nativeFillWindow(Native Method)
10-29 15:21:07.953: E/SQL order detail(23133):  at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:86)
10-29 15:21:07.953: E/SQL order detail(23133):  at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
10-29 15:21:07.953: E/SQL order detail(23133):  at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
10-29 15:21:07.953: E/SQL order detail(23133):  at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:161)
10-29 15:21:07.953: E/SQL order detail(23133):  at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:201)
10-29 15:21:07.953: E/SQL order detail(23133):  at de.l_one.app.shaufel.OrderDetailView$3.run(OrderDetailView.java:138)

10-29 15:22:52.983: E/AndroidRuntime(23133): FATAL EXCEPTION: Thread-1411
10-29 15:22:52.983: E/AndroidRuntime(23133): java.lang.IllegalStateException: database /data/data/de.l_one.app.shaufel/databases/poidatabase (conn# 0) already closed
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:58)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1538)
10-29 15:22:52.983: E/AndroidRuntime(23133):    at de.l_one.app.shaufel.OrderDetailView$3.run(OrderDetailView.java:132)

这些不是一次性抛出的,而是(看起来是)随机抛出的。抛出哪个异常没有模式,有时根本没有抛出异常。
我在我的应用程序的任何地方都使用相同的方法,并且它没有任何问题。我没有最小的线索IllegalStateException来自哪里,因为数据库没有关闭,至少我没有。(根据一些关于 SO 的答案是可以的,在某些情况下甚至比手动关闭你的数据库更可取)。

代码注释:
ShaufelDatabaseHelper是一个扩展SQLiteOpenHelper. 它用于通常工作的不同活动,这似乎是唯一的例外。

我刚刚在第二台设备上进行了测试,没有抛出异常并且一切正常。
抛出异常的设备是运行 android 4.0.3 的 lenovo thinkpad。
没有异常运行的是运行 Android 2.3.4 的 HTC Sensation Z710e。

有谁知道可能导致这种情况的原因?

E:我不知道它是否有帮助,但这是在之前记录的SQLiteMisuseException

10-29 16:07:46.530: I/SqliteDatabaseCpp(23728): sqlite returned: error code = 21, msg = API called with NULL prepared statement, db=/data/data/de.l_one.app.shaufel/databases/poidatabase
10-29 16:07:46.530: I/SqliteDatabaseCpp(23728): sqlite returned: error code = 21, msg = misuse at line 58929 of [8609a15dfa], db=/data/data/de.l_one.app.shaufel/databases/poidatabase
10-29 16:07:46.530: I/SqliteDatabaseCpp(23728): sqlite returned: error code = 21, msg = API call with invalid database connection pointer, db=/data/data/de.l_one.app.shaufel/databases/poidatabase
10-29 16:07:46.530: I/SqliteDatabaseCpp(23728): sqlite returned: error code = 21, msg = misuse at line 106340 of [8609a15dfa], db=/data/data/de.l_one.app.shaufel/databases/poidatabase
10-29 16:07:46.530: I/SqliteDatabaseCpp(23728): sqlite returned: error code = 21, msg = API call with invalid database connection pointer, db=/data/data/de.l_one.app.shaufel/databases/poidatabase
10-29 16:07:46.530: I/SqliteDatabaseCpp(23728): sqlite returned: error code = 21, msg = misuse at line 106271 of [8609a15dfa], db=/data/data/de.l_one.app.shaufel/databases/poidatabase
4

1 回答 1

10

原因

如果有任何机会另一个线程在您调用之间getReadableDatabase()rawQuery()或在rawQuery()您执行期间关闭了数据库,则会收到此异常。这是因为你SQLiteOpenHelper是一个单例(它应该是),这意味着如果某个其他线程关闭了数据库,mDatabase那么持有数据库连接的单个内部成员将被关闭。

我的建议:

对您的衍生产品实施您自己的close()方法。在您的实现中,在实际关闭它(调用)SQLiteOpenHelper之前检查打开的连接的引用计数。SQLiteOpenHelper.close()

private int openedConnections = 0;
//...
public synchronized SQLiteDatabase getReadableDatabase() {
    openedConnections++;
    getReadableDatabase();
}

public synchronized void close() {
    openedConnections--;
    if (openedConnections == 0) {
        close();
    }
}

让我知道它是否有效!:)

于 2012-12-23T14:30:35.890 回答