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

2023-09-05 23:43:20 作者:不是爱人不配情深

我正在使用SQLite数据库,并使用亚历克斯·洛克伍德的正确管理您的SQLite数据库

I am working with sqlite db and use some code of Alex LockWood Correctly Managing Your SQLite Database

它工作得很好,但有时我得到了错误java.lang.IllegalStateException:不能执行此操作,因为连接池已关闭 以下是完整的错误:

It works very well but sometimes I got the error "java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed." Here is the full Error:

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

这里是code导致该错误:

And here is the code that causes the error:

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;
    }

我试着在Google上搜寻了好几个小时,但没有结果。请帮我解决它。任何帮助将AP preciated。谢谢!

I tried googling for many hours but no result. Please help me to fix it. Any help will be appreciated. Thank you!

推荐答案

我也处理这个问题。我有事业的想法,很想听听专家们的这个观点。我做相当多的假设,来不受举证支持了一些结论....

   

我觉得异常被抛出的返回stadiumStatus行。一对夫妇线以上,您分配这个变量光标的一员。我相信,你实际上是在分配名称stadiumStatus指向只存在光标放在一个int变量。另一种可能(这里的逻辑的巨大的飞跃)是()由Cursor.getInt返回的项目实际上是一个指向一个项目(方法?)在打开的数据库(驻留在内存中,因为它是开放的。)无论哪种方式,当您关闭光标和/或数据库,然后尝试stadiumStatus,整型点返回地址的在的光标或通过了的光标,试图遵循它,并且无论是关闭的游标或关闭数据库死角(这些是数据池你是从图纸之一。)

   

如果我是正确的,我认为无论是最好的解决办法是更换:

I'm also dealing with this problem. I have an idea of the cause, and would love to hear the experts' opinions on this. I make quite a few assumptions and come to some conclusions not backed by proof....Superfetch 服务因下列错误而停止 操作系统当前的配置不能运行此应用程序 7023错误

I think the exception is thrown on the 'return stadiumStatus' line. A couple of lines above, you assign this variable to a member of the Cursor. I believe that you are actually assigning the name stadiumStatus to point to an int variable that only exists inside the cursor. Another possibility (huge leap of logic here) is that the item returned by Cursor.getInt() is actually a pointer to an item (a method?) in the open database (which resides in memory because it is open.) Either way, when you close the Cursor and/or database and then try to return stadiumStatus, the int points to the address in the Cursor or passed by the Cursor, tries to follow it, and dead ends at either the closed Cursor or the closed Database (one of these is the "data pool" you are drawing from.)

If I am right, I think the best fix for either would be to replace:

stadiumStatus = cur.getInt(11);

"stadiumStatus = cur.getInt(11);"

stadiumStatus =新的整数(cur.getInt(11));

"stadiumStatus = new Integer(cur.getInt(11));"

因此​​在内存中创建一个新的整数虚拟机可以平凡转换为一个int,而这是只依赖于它的传统范围的可访问性。

   

难道这个答案有意义吗?我敢肯定,发问早已移过这个问题,但我很想能够说我回答的问题上堆栈溢出(或将要告诉我为什么错了,有一个更好的理解游标和DB年代)。

   

编辑:这些答案意味着我可能是对的:       Android错误:不能执行此操作,因为连接池已关闭       Will光标还活着后,数据库关闭?    在第二个答案,用户DeeV,谁拥有巨大的声誉,呼吁光标永久连接,这意味着它不是一个简单的集合返回的值从数据库中。

thereby creating a new Integer in memory which the vm can trivially cast to an int, and which is reliant only on its traditional scope for accessibility.

Does this answer make sense? I'm sure the questioner has long since moved past this problem, but I would love be able to say I answered a question on Stack Overflow (or to be told why I am wrong, and have a better understanding of Cursors and DB's).

These answers imply I may be right: Android error: Cannot perform this operation because the connection pool has been closed Will cursor be still alive after database is closed? In the second answer, user DeeV, who has a huge reputation, calls Cursors "persistent connections", implying it is not a simple collection of returned values from the DB.

 
精彩推荐
图片推荐