Skip to content

Commit b994a9b

Browse files
committed
modifications to open helper for RO/RW db management
1 parent 276126d commit b994a9b

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/net/sqlcipher/database/SQLiteOpenHelper.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public abstract class SQLiteOpenHelper {
3939
private final CursorFactory mFactory;
4040
private final int mNewVersion;
4141

42-
private SQLiteDatabase mDatabase = null;
42+
private SQLiteDatabase mDatabaseRO, mDatabaseRW = null;
4343
private boolean mIsInitializing = false;
4444

4545
/**
@@ -75,8 +75,8 @@ public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int
7575
* @return a read/write database object valid until {@link #close} is called
7676
*/
7777
public synchronized SQLiteDatabase getWritableDatabase(String password) {
78-
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
79-
return mDatabase; // The database is already open for business
78+
if (mDatabaseRW != null && mDatabaseRW.isOpen()) {
79+
return mDatabaseRW; // The database is already open for business
8080
}
8181

8282
if (mIsInitializing) {
@@ -91,7 +91,7 @@ public synchronized SQLiteDatabase getWritableDatabase(String password) {
9191

9292
boolean success = false;
9393
SQLiteDatabase db = null;
94-
if (mDatabase != null) mDatabase.lock();
94+
if (mDatabaseRW != null) mDatabaseRW.lock();
9595
try {
9696
mIsInitializing = true;
9797
if (mName == null) {
@@ -135,13 +135,13 @@ public synchronized SQLiteDatabase getWritableDatabase(String password) {
135135
} finally {
136136
mIsInitializing = false;
137137
if (success) {
138-
if (mDatabase != null) {
139-
try { mDatabase.close(); } catch (Exception e) { }
140-
mDatabase.unlock();
138+
if (mDatabaseRW != null) {
139+
try { mDatabaseRW.close(); } catch (Exception e) { }
140+
mDatabaseRW.unlock();
141141
}
142-
mDatabase = db;
142+
mDatabaseRW = db;
143143
} else {
144-
if (mDatabase != null) mDatabase.unlock();
144+
if (mDatabaseRW != null) mDatabaseRW.unlock();
145145
if (db != null) db.close();
146146
}
147147
}
@@ -161,21 +161,14 @@ public synchronized SQLiteDatabase getWritableDatabase(String password) {
161161
* or {@link #close} is called.
162162
*/
163163
public synchronized SQLiteDatabase getReadableDatabase(String password) {
164-
if (mDatabase != null && mDatabase.isOpen()) {
165-
return mDatabase; // The database is already open for business
164+
if (mDatabaseRO != null && mDatabaseRO.isOpen()) {
165+
return mDatabaseRO; // The database is already open for business
166166
}
167167

168168
if (mIsInitializing) {
169169
throw new IllegalStateException("getReadableDatabase called recursively");
170170
}
171171

172-
try {
173-
return getWritableDatabase(password);
174-
} catch (SQLiteException e) {
175-
if (mName == null) throw e; // Can't open a temp database read-only!
176-
Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
177-
}
178-
179172
SQLiteDatabase db = null;
180173
try {
181174
mIsInitializing = true;
@@ -188,11 +181,11 @@ public synchronized SQLiteDatabase getReadableDatabase(String password) {
188181

189182
onOpen(db);
190183
Log.w(TAG, "Opened " + mName + " in read-only mode");
191-
mDatabase = db;
192-
return mDatabase;
184+
mDatabaseRO = db;
185+
return mDatabaseRO;
193186
} finally {
194187
mIsInitializing = false;
195-
if (db != null && db != mDatabase) db.close();
188+
if (db != null && db != mDatabaseRO) db.close();
196189
}
197190
}
198191

@@ -202,9 +195,14 @@ public synchronized SQLiteDatabase getReadableDatabase(String password) {
202195
public synchronized void close() {
203196
if (mIsInitializing) throw new IllegalStateException("Closed during initialization");
204197

205-
if (mDatabase != null && mDatabase.isOpen()) {
206-
mDatabase.close();
207-
mDatabase = null;
198+
if (mDatabaseRO != null && mDatabaseRO.isOpen()) {
199+
mDatabaseRO.close();
200+
mDatabaseRO = null;
201+
}
202+
203+
if (mDatabaseRW != null && mDatabaseRW.isOpen()) {
204+
mDatabaseRW.close();
205+
mDatabaseRW = null;
208206
}
209207
}
210208

0 commit comments

Comments
 (0)