数据库开启的并发性

时间:2014-01-15 09:28:28

标签: java concurrency java-memory-model

我不知道如何进行正确的并发操作,所以我只是尝试调整我的代码。 完全失去了构造函数中的并发性以及静态的最终字段的并发性......

public static class Connection {
        private final CustomerDatabaseOpenHelper mHelper;
        private SQLiteDatabase mDatabase;
        private String mUserId;
        private AtomicInteger mOpenCounter;

        public Connection(Context context, String userId) {
            mHelper =  CustomerDatabaseOpenHelper.getInstance(context, DB_NAME, null, DB_VERSION);
            mOpenCounter = mHelper.mOpenCounter;
            mUserId = userId;
        }

        public void open() throws SQLException {
            // open database in reading/writing mode
            int value = mOpenCounter.incrementAndGet();
             if(value == 1 || mDatabase==null || mUserId ==null) {
                    // Opening new database
                 Log.v("tugce","open new customer db");

                 mDatabase = mHelper.getWritableDatabase();
                }

        };

        public void close() {
            int value = mOpenCounter.decrementAndGet();
             if(value == 0) {
                    // Closing database
                 if (mDatabase != null) {
                        try {
                             Log.v("tugce","close customer db");
                            mDatabase.close();
                            mDatabase = null;
                            mUserId = null;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
        }

//我的openhelper类

public class CustomerDatabaseOpenHelper extends SQLiteOpenHelper{

    public AtomicInteger mOpenCounter = new AtomicInteger();
    public static CustomerDatabaseOpenHelper mInstance;

    public static synchronized CustomerDatabaseOpenHelper getInstance(Context context, String name,
            CursorFactory factory, int version) {

        if (mInstance == null) {
            mInstance = new CustomerDatabaseOpenHelper(context, name, factory, version);
        }
        return mInstance;
      }

    private CustomerDatabaseOpenHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
    }

这就是我在线程中使用的方式:

class MyGetPlanedCallList extends
            AsyncTask<Object, Object, List<MyPlannedCallListItem>> {
        CustomerDatabase.Connection mDbCon = new CustomerDatabase.Connection(InstanceActivity.this, mUserId);

        @Override
        protected void onPreExecute() {
            mDbCon.open();
        }

        @Override
        protected List<MyPlannedCallListItem> doInBackground(Object... params) {
            mDbCon.doSomeStuff();
        }



        @Override
        protected void onPostExecute(List<MyPlannedCallListItem> result) {
            mDbCon.close();
        }

        @Override
        protected void onCancelled() {
            if (mDbCon != null)
                mDbCon.close();
        }
    }

我想要的是,如果某个其他实例正在处理它,则不应该关闭完全相同的数据库。

任何帮助都将不胜感激。

0 个答案:

没有答案