繁体   English   中英

单次运行应用程序只创建一次greenDAO数据库连接的最佳方法是什么?

[英]What is the best way of creating greenDAO DB connection only once for single run of application?

目前我正在一个类中创建greenDAO数据库连接(它在每个静态方法中打开连接)并在我需要的地方使用它。 但我不确定这是否是最佳方式。 谁能建议一个更好的方法呢?

我的代码:

import com.knowlarity.sr.db.dao.DaoMaster;
import com.knowlarity.sr.db.dao.DaoMaster.DevOpenHelper;
import com.knowlarity.sr.db.dao.DaoSession;
import com.knowlarity.sr.db.dao.IEntity;

public class DbUtils {

    private static Object lockCallRecord =new Object();
    private DbUtils(){};

    public static boolean saveEntity(Context context , IEntity entity){
            boolean t=false;
            DevOpenHelper helper=null;
            SQLiteDatabase db=null;
            DaoMaster daoMaster=null;
            DaoSession daoSession =null;
            try{
               helper = new DaoMaster.DevOpenHelper(context, IConstant.DB_STRING, null);
               db = helper.getReadableDatabase();
               daoMaster = new DaoMaster(db);
               daoSession = daoMaster.newSession();
               //Some business logic here for fetching and inserting the data.
            }catch (Exception e){
               Log.e("saveEntity", e.getStackTrace().toString());
            }finally{
               if(daoSession!=null)daoSession.clear();
               daoMaster=null;
               if(db.isOpen())db.close();
               helper.close();
            }
            return t;
    }

您的方法导致数据库经常被加载,这是不必要的,可能会显着减慢您的应用程序。

打开数据库一次并将其存储在某处,并在需要时从那里请求它。

我个人使用全球DaoSession和本地DaoSession 本地DaoSession被用于会话缓存中不应该保留任何内容(即将新对象持久存储到数据库中,这可能只是很少使用或者执行某些查询会加载很多不太可能被重用的实体再次)。

请记住,如果您在全局会话中使用该实体,那么更新本地DaoSession中的实体也是一个坏主意。 如果这样做,全局会话中的缓存实体将不会更新,除非您清除全局会话的缓存,否则您将得到错误的结果!

因此,最安全的方法是始终只使用一个DaoSession或新的DaoSession ,并且不使用全局和本地会话!

自定义应用程序类是个好地方,但任何其他类也都可以。

我是这样做的:

class DBHelper:

private SQLiteDatabase _db = null;
private DaoSession _session = null;

private DaoMaster getMaster() {
    if (_db == null) {
        _db = getDatabase(DB_NAME, false);
    }
    return new DaoMaster(_db);
}

public DaoSession getSession(boolean newSession) {
    if (newSession) {
        return getMaster().newSession();
    }
    if (_session == null) {
        _session = getMaster().newSession();
    }
    return _session;
}

private synchronized SQLiteDatabase getDatabase(String name, boolean readOnly) {
    String s = "getDB(" + name + ",readonly=" + (readOnly ? "true" : "false") + ")";
    try {
        readOnly = false;
        Log.i(TAG, s);
        SQLiteOpenHelper helper = new MyOpenHelper(context, name, null);
        if (readOnly) {
            return helper.getReadableDatabase();
        } else {
            return helper.getWritableDatabase();
        }
    } catch (Exception ex) {
        Log.e(TAG, s, ex);
        return null;
    } catch (Error err) {
        Log.e(TAG, s, err);
        return null;
    }
}

private class MyOpenHelper extends DaoMaster.OpenHelper {
    public MyOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG, "Create DB-Schema (version "+Integer.toString(DaoMaster.SCHEMA_VERSION)+")");
        super.onCreate(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(TAG, "Update DB-Schema to version: "+Integer.toString(oldVersion)+"->"+Integer.toString(newVersion));
        switch (oldVersion) {
            case 1:
                db.execSQL(SQL_UPGRADE_1To2);
            case 2:
                db.execSQL(SQL_UPGRADE_2To3);
                break;
            default:
                break;
        }
    }
}

在应用程序类中:

private static MyApplication _INSTANCE = null;

public static MyApplication getInstance() {
    return _INSTANCE;
}

@Override
public void onCreate() {
    _INSTANCE = this;
    // ...
}

private DBHelper _dbHelper = new DBHelper();

public static DaoSession getNewSession() {
    return getInstance()._dbHelper.getSession(true);
}

public static DaoSession getSession() {
    return getInstance()._dbHelper.getSession(false);
}

当然,您也可以存储DaoMaster而不是DB本身。 这将减少一些小的开销。

我每次使用一些常用方法(比如访问数据库)时都使用类似Singleton的Application类和静态方法来避免转换应用程序( ((MyApplication)getApplication()) )。

我建议在Application类中创建数据库。 然后,您可以创建一个方法来返回DaoSession以访问其他活动中的数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM