android – 错误无法执行此操作,因为连接池已关闭

前端之家收集整理的这篇文章主要介绍了android – 错误无法执行此操作,因为连接池已关闭前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用sqlite数据库,并使用Alex LockWood Correctly Managing Your SQLite Database的一些代码

它工作得很好,但有时我得到错误“java.lang.IllegalStateException:无法执行此操作,因为连接池已关闭.
这是完整的错误

02-20 16:37:21.385: W/dalvikvm(25730): threadid=13: thread exiting with uncaught exception (group=0x41c122a0)
02-20 16:37:21.390: E/AndroidRuntime(25730): FATAL EXCEPTION: Timer-0
02-20 16:37:21.390: E/AndroidRuntime(25730): java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteConnectionPool.throwIfClosedLocked(sqliteConnectionPool.java:963)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteConnectionPool.waitForConnection(sqliteConnectionPool.java:678)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteConnectionPool.acquireConnection(sqliteConnectionPool.java:349)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteSession.acquireConnection(sqliteSession.java:894)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteSession.executeForCursorWindow(sqliteSession.java:834)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteQuery.fillWindow(sqliteQuery.java:62)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteCursor.fillWindow(sqliteCursor.java:143)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at android.database.sqlite.sqliteCursor.getCount(sqliteCursor.java:133)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at com.uit.pokemon.DatabaseHandler.getStadiumStatusById(DatabaseHandler.java:533)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at playground.RoomActivity.checkTable(RoomActivity.java:276)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at playground.RoomActivity$6.run(RoomActivity.java:321)
02-20 16:37:21.390: E/AndroidRuntime(25730):    at java.util.Timer$TimerImpl.run(Timer.java:284)
02-20 16:37:21.460: I/timertask cancel(25730): canceled

这里是导致错误代码

public int getStadiumStatusById(int dataStadiumId){
        sqliteDatabase db = this.getReadableDatabase();
        Cursor cur = db.rawQuery("SELECT " + keyStadiumId + " as _id,"
                + keyRoomName + "," + keyP1Name + "," + keyP1PokemonName
                + "," + keyP1PokemonLevel + "," + keyP1PokemonHp + ","
                + keyP2Name + "," + keyP2PokemonName + ","
                + keyP2PokemonLevel + "," + keyP2PokemonHp + ","
                + keyTimeCreate + "," + keyStadiumStatus + " from "
                + tbl_stadium + " WHERE " + keyStadiumId + " = " + "'"
                + dataStadiumId + "'",new String[] {});

        int stadiumStatus = 0;
        if(cur.getCount()>0)
        {
        cur.moveToFirst();
        stadiumStatus = cur.getInt(11);
        }
        db.close();
        cur.close();
        return stadiumStatus;
    }

我尝试了几个小时的谷歌搜索,但没有结果.请帮我解决.任何帮助将不胜感激.谢谢!

解决方法

我也在处理这个问题.我有一个理由,并且很乐意听取专家的意见.我做了很多假设,并得出一些不支持证据的结论.
我认为这个例外是在“返回体育馆状态”行上抛出的.在上面的几行中,您将此变量分配给Cursor的成员.我相信你实际上分配名称stadiumStatus来指向只存在于游标中的int变量.另一个可能性(这里的巨大飞跃)是Cursor.getInt()返回的项目实际上是一个指向开放数据库中的一个项目(一个方法?)的指针(由于它是开放的,它驻留在内存中).无论哪种方式,当您关闭Cursor和/或数据库,然后尝试返回stadiumStatus时,int指向Cursor中的地址或由Cursor传递,尝试跟随它,并在封闭的Cursor或封闭数据库(一个这些是您正在绘制的“数据池”.)
如果我是对的,我认为最好的解决办法是替换:

“stadiumStatus = cur.getInt(11);”

有:

“stadiumStatus = new Integer(cur.getInt(11));”

从而在内存中创建一个新的Integer,这个vm可以简单地转换为int,并且只依赖于其传统的可访问性范围.
这个答案是否有意义?我确信提问者已经过了这个问题,但我很想说可以说我回答了一个关于Stack Overflow的问题(或被告知为什么我错了,并且更好地了解了Cursors和DB).
编辑:这些答案暗示我可能是对的:
Android error: Cannot perform this operation because the connection pool has been closed
Will cursor be still alive after database is closed?在第二个答案中,具有巨大声望的用户DeeV称之为游标“持久连接”,这意味着它不是DB中返回值的简单集合.

原文链接:https://www.f2er.com/android/313067.html

猜你在找的Android相关文章