目前我正在一個類中創(chuàng)建greenDAO數據庫連接(它在每個靜態(tài)方法中打開連接)并在我需要的地方使用它.但我不確定這是否是最佳方式. 誰能建議一個更好的方法呢,?
我的代碼:
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和本地DaoSessions.本地DaoSessions被用于會話緩存中不應該保留任何內容(即將新對象持久存儲到數據庫中,這可能只是非常罕見地使用或者執(zhí)行一些查詢會加載很多不太可能再次被重用的實體).
請記住,如果您在全局會話中使用該實體,那么更新本地DaoSession中的實體也是一個壞主意.如果這樣做,全局會話中的緩存實體將不會更新,除非您清除全局會話的緩存,否則您將得到錯誤的結果,!
因此,最安全的方法是始終只使用一個DaoSession或新的DaoSessions并且不使用全局和本地會話,!
自定義應用程序類是個好地方,但任何其他類也都可以.
我是這樣做的:
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類和靜態(tài)方法來避免轉換應用程序(((MyApplication)getApplication())). 來源:https://www./content-2-336601.html
|