Android数据库帮助程序单例和同步

时间:2016-03-16 11:29:51

标签: android sqlite synchronization singleton

我使用SQLiteOpenHelper创建了一个DB帮助器。我已经在Singleton模式中创建了相同的东西来停止创建多个数据库对象。由于存在来自多个线程的数据库操作,我维护对象创建线程安全,我使其同步。

    public class DatabaseManager{


    private static final String     DATABASE_NAME = "dbname";

    private static final int        DATABASE_VERSION = 1;

    private DatabaseHelper   helper;
    private SQLiteDatabase   db;
    private Context          context;

    private DatabaseManager(Context c){
        this.context = c;
        helper = new DatabaseHelper(context); 
    }


    private static final Object lock = new Object();
    private static volatile DatabaseManager instance;

    public static DatabaseManager getInstance(Context c){
    DatabaseManager r = instance;
    if (r == null) {
        synchronized (lock) {    // While we were waiting for the lock, another 
            r = instance;        // thread may have instantiated the object.
            if (r == null) {  
                r = new DatabaseManager();
                instance = r;
            }
        }
    }
    return r;
   }




    public Cursor getAll() {
            SQLiteDatabase db = _openHelper.getReadableDatabase();
            if (db == null) {
                return null;
            }
            return db.rawQuery("select * from todos order by priority, title", null);
    }

    public void delete(long id) {
            SQLiteDatabase db = _openHelper.getWritableDatabase();
            if (db == null) {
                return;
            }
            db.delete("todos", "_id = ?", new String[] { String.valueOf(id) });
            db.close();
    }

    public long add(String title, int priority) {
            SQLiteDatabase db = _openHelper.getWritableDatabase();
            if (db == null) {
                return 0;
            }
            ContentValues row = new ContentValues();
            row.put("title", title);
            row.put("priority", priority);
            long id = db.insert("todos", null, row);
            db.close();
            return id;
    }

    public void update(long id, String title, int priority) {
            SQLiteDatabase db = _openHelper.getWritableDatabase();
            if (db == null) {
                return;
            }
            ContentValues row = new ContentValues();
            row.put("title", title);
            row.put("priority", priority);
            db.update("todos", row, "_id = ?", new String[] { String.valueOf(id) } );
            db.close();
    }


    class DatabaseHelper extends SQLiteOpenHelper{

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

        @Override
        public void onCreate(SQLiteDatabase db){            
                db.execSQL("create table todos (_id integer primary key autoincrement, title text, priority integer)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

        }
    }
}

现在我是否应该同步get(),add(),update(),delete()方法以使它们成为线程安全的?

1 个答案:

答案 0 :(得分:1)

SqliteDatabase is thread safe by default所以你不应该同步get,update等功能。

另请查看SqliteDatabase synchronization

顺便说一下,你实际上正在创建一个Content Provider。为什么不使用ContentProvider类?