Code example for ReentrantLock

Methods: lock, unlock

0
        boolean success = false;
        SQLiteDatabase db = null;
        boolean doUnlock = false;
        if (mDatabase != null) {
        	doUnlock = true;
        	mLock.lock();
        } 
        try { 
            mIsInitializing = true;
            if (mDbFilePath == null) {
                db = SQLiteDatabase.create(null);
            } else { 
                db = SQLiteDatabase.openOrCreateDatabase(mDbFilePath, mFactory);
            } 
 
            int version = db.getVersion();
            if (version != mNewVersion) {
                db.beginTransaction();
                try { 
                    if (version == 0) {
                        onCreate(db);
                    } else { 
                        onUpgrade(db, version, mNewVersion);
                    } 
                    db.setVersion(mNewVersion);
                    db.setTransactionSuccessful();
                } finally { 
                    db.endTransaction();
                } 
            } 
 
            onOpen(db);
            success = true;
            return db;
        } finally { 
            mIsInitializing = false;
            if (success) {
                if (mDatabase != null) {
                    try { mDatabase.close(); } catch (Exception e) { }
                } 
                if (doUnlock)
	                mLock.unlock();
                mDatabase = db;
            } else { 
            	if (doUnlock)
	                mLock.unlock();
                if (db != null) db.close();
            } 
        } 
    }