I am working with sqlite db and use some code of Alex LockWood Correctly Managing Your SQLite Database
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
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;
}
I tried googling for many hours but no result. Please help me to fix it. Any help will be appreciated. Thank you!
In your code you are using:
db.close();
cur.close();
The best practice is to close the cursor first and then closing the database as once you call db.close()
, it will make the database freeze n closed and its corresponding cursor invalid. Try this change it will work.
Replace it by:
cur.close();
db.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With