gpt4 book ai didi

android - 尽管一切都关闭了,但 SQLite 连接泄漏了

转载 作者:IT老高 更新时间:2023-10-28 21:35:40 26 4
gpt4 key购买 nike

我发现了很多类似 close the connectionclose the cursor 的东西,但这些都是我做的。 SQLite 连接仍然泄漏,我收到如下警告:

A SQLiteConnection object for database was leaked!

我有一个数据库管理器,我用以下代码在我的 Activity 中调用它:

DatabaseManager dbm = new DatabaseManager(this);

我的数据库管理器类的代码如下:

public class DatabaseManager {

private static final int DATABASE_VERSION = 9;
private static final String DATABASE_NAME = "MyApp";
private Context context = null;
private DatabaseHelper dbHelper = null;
private SQLiteDatabase db = null;


public static class DatabaseHelper extends SQLiteOpenHelper {

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

@Override
public void onCreate(SQLiteDatabase db) {

//create database tables
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//destroy and recreate them
}

}

public DatabaseManager(Context ctx) {
this.context = ctx;
}

private DatabaseManager open() throws SQLException {
dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();

if (!db.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys = ON;");
}

return this;
}

private void close() {
dbHelper.close();
}
}

当我调用数据库方法时,我会做以下事情:

public Object getData() {

open();

//... database operations take place ...

close();

return data;
}

但正如我所说,我仍然收到此 SQLite 连接泄漏警告。

我做错了什么?

最佳答案

引用中的加粗字体对应于您代码中的这一部分:

private DatabaseManager open() throws SQLException {
dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();

来自:http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html

Approach #1: Use an Abstract Factory to Instantiate the SQLiteOpenHelper

Declare your database helper as a static instance variable and use the Abstract Factory pattern to guarantee the singleton property. The sample code below should give you a good idea on how to go about designing the DatabaseHelper class correctly.

The static factory getInstance method ensures that only one DatabaseHelper will ever exist at any given time. If the mInstance object has not been initialized, one will be created. If one has already been created then it will simply be returned.

You should not initialize your helper object using with new DatabaseHelper(context).
Instead, always use DatabaseHelper.getInstance(context), as it guarantees that only one database helper will exist across the entire application's lifecycle.

public static class DatabaseHelper extends SQLiteOpenHelper { 

private static DatabaseHelper mInstance = null;

private static final String DATABASE_NAME = "database_name";
private static final String DATABASE_TABLE = "table_name";
private static final int DATABASE_VERSION = 1;

public static DatabaseHelper getInstance(Context ctx) {

// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (mInstance == null) {
mInstance = new DatabaseHelper(ctx.getApplicationContext());
}
return mInstance;
}

/**
* Constructor should be private to prevent direct instantiation.
* make call to static factory method "getInstance()" instead.
*/
private DatabaseHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
}

关于android - 尽管一切都关闭了,但 SQLite 连接泄漏了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18147354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com