Android:无法执行此操作,因为连接池已关闭
我正在阅读StackOverflow关于这个问题,我仍然没有找到解决方案。我注意到有时,我的应用程序会抛出此错误:
java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:962)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:599)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:348)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
...
我有一个名为使用此方法的文件来获取它的实例:DatabaseHelper.java
public static DatabaseHelper getInstance(Context context) {
if (mInstance == null) {
mInstance = new DatabaseHelper(context.getApplicationContext());
}
return mInstance;
}
然后我有这样的方法(它在带有该错误的行中崩溃)。它几乎从不崩溃,但有时它确实如此。cursor.moveToFirst()
public Profile getProfile(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_PROFILES + " WHERE " + KEY_PROFILES_ID + " = " + id;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
Profile profile = new Profile();
if (cursor.moveToFirst()) {
doWhatEver();
}
cursor.close();
db.close();
return profile;
}
就是这样,在我使用的所有方法中:
SQLiteDatabase db = this.getReadableDatabase(); (or Writable)
然后我关闭光标和数据库。在本例中,错误被扔到行中:
cursor.moveToFirst();
我不明白为什么错误说db关闭,如果我之前正在调用。请支持!谢谢:)this.getReadableDatabase()