【问题标题】:Android SQLite issue "Cannot perform this operation because the connection pool has been closed"Android SQLite 问题“无法执行此操作,因为连接池已关闭”
【发布时间】:2016-02-11 22:20:54
【问题描述】:

我有时会遇到 SQLite 问题,有时我的应用会崩溃,错误是

 Caused by: java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.

这是我的错误来自的函数

public List<contacts> getAllcontacts() {
List<contacts> contactsl = new LinkedList<contacts>();

// 1. build the query
String query = "SELECT  * FROM contacts";

// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);

// 3. go over each row, build a contact and add it to list
contacts contact = null;
if (cursor.moveToFirst()) {
    do {
        contact = new contacts();

        contact.setName(cursor.getString(1));
        contact.setNumero(cursor.getString(3));
        contact.setProfil(cursor.getString(2));
        contact.setShow(cursor.getString(5));
        contact.setBlocked(cursor.getString(4));
        contact.setObjectid(cursor.getString(6));
        contactsl.add(contact);
    } while (cursor.moveToNext());
}

return contactsl;
}

当我在测试我的应用程序并尝试多次启动该功能时执行该功能时显示错误。

这里是数据库的创建:

private static sql sInstance;

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "Contact";

public static sql getInstance(Context context) {

// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
if (sInstance == null) {
    sInstance = new sql(context.getApplicationContext());
}
return sInstance;
}

public sql(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATETABLE = "CREATE TABLE contacts ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "name TEXT, "+
            "profil TEXT, "+
            "phone TEXT UNIQUE ,"+
            "blocked TEXT, "+
            "show TEXT , "+
            "objectid TEXT )";

    db.execSQL(CREATETABLE);
}




@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int `newVersion) {`
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS contacts");

    this.onCreate(db);
}

【问题讨论】:

  • 你是如何实例化你的数据库助手的?
  • 帖子更新了我的数据库的所有创建

标签: android sqlite


【解决方案1】:

如果您过早关闭 DBHelper,您可能会收到此错误。如果您关闭 Helper 并关闭它,请搜索您的代码并查看您是否仍然遇到该错误。

否则,也许可以试试这个模板来实例化您的 DBHelper。

public class MyDBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "myDatabase.db";
    private static final int SCHEMA = 1;
    private static volatile MyDBHelper sInstance;


    private MyDBHelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA);
    }

    public static MyDBHelper getInstance(Context context) {
        if (sInstance == null) {
            synchronized (MyDBHelper.class) {
                if (sInstance == null) {
                    sInstance = new MyDBHelper(context.getApplicationContext());
                }
            }
        }
        return sInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // create your tables here
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // handle db upgrade or leave blank
    }
}

作为对您评论的回答,您仍然可以在实例完成后关闭数据库

@Override
public void finalize() throws Throwable {
    sInstance.close();
    super.finalize();
}

【讨论】:

  • 所以我不应该在我的所有 SQLite 类中使用“db.close”?
  • 保持连接打开对性能的影响很小,当然,当您确定数据库已完成时,您应该关闭它。
  • 当我覆盖了您在编辑后的答案中提到的 finalize 函数时,我在日志中有这个:I/Choreographer:跳过了 33 帧!应用程序可能在其主线程上做了太多工作。
猜你喜欢
  • 2015-02-27
  • 2015-07-12
  • 2013-04-10
  • 1970-01-01
  • 2014-06-16
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多