Android的:不能执行此操作,因为连接池已关闭操作、连接池、Android

2023-09-05 06:49:54 作者:故事配酒你配狗

我读通过计算器关于这个问题,我还没有找到一个解决方案。我注意到,有时,我的应用程序引发此错误:

I was reading through stackoverflow about this question and I still haven't found a solution. I notice that sometimes, my app throws this error:

   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文件:

I have a file called DatabaseHelper.java using this approach to get an instance of it:

public static DatabaseHelper getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return mInstance;
}

然后,我有像这样的方法(它在该行cursor.moveToFirst()与该错误崩溃)。它几乎从来没有崩溃,但有时它。

Then I have methods like this one (that it crashed in the line cursor.moveToFirst() with that error). It almost never crashes but sometimes it does.

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

所以这是它,在所有的方法我用的:

So that's it, in all the methods I use:

SQLiteDatabase db = this.getReadableDatabase(); (or Writable)

然后我关闭游标和DB。在这种情况下,该错误得到扔在线路:

And then I close the cursor and the db. In this case, the error got throw in the line:

cursor.moveToFirst();

我不明白为什么错误说,如果我是()之前调用this.getReadableDatabase分贝关闭。请大家支持!谢谢:)

I do not see why the error says the db is closed if I am calling this.getReadableDatabase() before. Please support! Thank you :)

推荐答案

删除

db.close();

如果您尝试关闭数据库陆续投产,它会给你的例外。

If you try another operation after closing the database, it will give you that exception.

的documentation说:

发布一个参考对象,关闭对象...

此外,检查出 的Andr​​oid平米精简版封闭异常

 
精彩推荐