Android SQLite DB 何时关闭 [英] Android SQLite DB When to Close

查看:22
本文介绍了Android SQLite DB 何时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 android 上使用 SQLite 数据库.我的数据库管理器是一个单例,现在在初始化时打开与数据库的连接.让数据库一直打开是安全的,这样当有人打电话给我的班级使用数据库时,它已经打开了?或者我应该在每次访问之前和之后打开和关闭数据库.一直开着有什么坏处吗?

I am working with a SQLite database on android. My database manager is a singleton and right now opens a connection to the database when it is initialized. It is safe to leave the database open the entire time so that when someone calls my class to work with the database it is already open? Or should I open and close the database before and after each access is needed. Is there any harm in just leaving it open the whole time?

谢谢!

推荐答案

我会一直保持打开状态,并在一些生命周期方法中关闭它,例如 onStoponDestroy.这样,您可以在每次使用之前通过在单个 SQLiteDatabase 对象上调用 isDbLockedByCurrentThreadisDbLockedByOtherThreads 来轻松检查数据库是否已在使用中它.这将防止对数据库的多次操作并使您的应用程序免于潜在的崩溃

i would keep it open the whole time, and close it in some lifecycle method such as onStop or onDestroy. that way, you can easily check if the database is already in use by calling isDbLockedByCurrentThread or isDbLockedByOtherThreads on the single SQLiteDatabase object every time before you use it. this will prevent multiple manipulations to the database and save your application from a potential crash

所以在你的单例中,你可能有这样的方法来获取你的单个 SQLiteOpenHelper 对象:

so in your singleton, you might have a method like this to get your single SQLiteOpenHelper object:

private SQLiteDatabase db;
private MyDBOpenHelper mySingletonHelperField;
public MyDBOpenHelper getDbHelper() {
    db = mySingletonHelperField.getDatabase();//returns the already created database object in my MyDBOpenHelper class(which extends `SQLiteOpenHelper`)
    while(db.isDbLockedByCurrentThread() || db.isDbLockedByOtherThreads()) {
        //db is locked, keep looping
    }
    return mySingletonHelperField;
}

所以无论何时你想使用你的 open helper 对象,调用这个 getter 方法(确保它是线程的)

so whenever you want to use your open helper object, call this getter method(make sure it's threaded)

单例中的另一种方法可能是(在您尝试调用上面的 getter 之前每次都调用):

another method in your singleton may be(called EVERY TIME before you try to call the getter above):

public void setDbHelper(MyDBOpenHelper mySingletonHelperField) {
    if(null == this.mySingletonHelperField) {
        this.mySingletonHelperField = mySingletonHelperField;
        this.mySingletonHelperField.setDb(this.mySingletonHelperField.getWritableDatabase());//creates and sets the database object in the MyDBOpenHelper class
    }
}

您可能还想在单例中关闭数据库:

you may want to close the database in the singleton as well:

public void finalize() throws Throwable {
    if(null != mySingletonHelperField)
        mySingletonHelperField.close();
    if(null != db)
        db.close();
    super.finalize();
}

如果您的应用程序的用户能够非常快速地创建许多数据库交互,那么您应该使用我上面演示的类似方法.但是如果数据库交互很少,我不会担心,每次都创建和关闭数据库.

if the users of your application have the ability to create many database interactions very quickly, you should use something like i have demonstrated above. but if there is minimal database interactions, i wouldn't worry about it, and just create and close the database every time.

这篇关于Android SQLite DB 何时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆