Merge pull request #4666 from ByteHamster/db-corruption
Use lazy singleton instead of Bill Pugh singleton
This commit is contained in:
commit
0e84f5d90c
|
@ -316,46 +316,44 @@ public class PodDBAdapter {
|
||||||
+ JOIN_FEED_ITEM_AND_MEDIA;
|
+ JOIN_FEED_ITEM_AND_MEDIA;
|
||||||
|
|
||||||
private static Context context;
|
private static Context context;
|
||||||
|
private static PodDBAdapter instance;
|
||||||
|
|
||||||
private static volatile SQLiteDatabase db;
|
private final SQLiteDatabase db;
|
||||||
|
private final PodDBHelper dbHelper;
|
||||||
|
|
||||||
public static void init(Context context) {
|
public static void init(Context context) {
|
||||||
PodDBAdapter.context = context.getApplicationContext();
|
PodDBAdapter.context = context.getApplicationContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bill Pugh Singleton Implementation
|
|
||||||
private static class SingletonHolder {
|
|
||||||
private static final PodDBHelper dbHelper = new PodDBHelper(PodDBAdapter.context, DATABASE_NAME, null);
|
|
||||||
private static final PodDBAdapter dbAdapter = new PodDBAdapter();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static PodDBAdapter getInstance() {
|
public static PodDBAdapter getInstance() {
|
||||||
return SingletonHolder.dbAdapter;
|
if (instance == null) {
|
||||||
|
instance = new PodDBAdapter();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private PodDBAdapter() {
|
private PodDBAdapter() {
|
||||||
|
dbHelper = new PodDBHelper(PodDBAdapter.context, DATABASE_NAME, null);
|
||||||
|
db = openDb();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized PodDBAdapter open() {
|
|
||||||
if (db == null || !db.isOpen() || db.isReadOnly()) {
|
|
||||||
db = openDb();
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
|
||||||
private SQLiteDatabase openDb() {
|
private SQLiteDatabase openDb() {
|
||||||
SQLiteDatabase newDb;
|
SQLiteDatabase newDb;
|
||||||
try {
|
try {
|
||||||
newDb = SingletonHolder.dbHelper.getWritableDatabase();
|
newDb = dbHelper.getWritableDatabase();
|
||||||
newDb.disableWriteAheadLogging();
|
newDb.disableWriteAheadLogging();
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
Log.e(TAG, Log.getStackTraceString(ex));
|
Log.e(TAG, Log.getStackTraceString(ex));
|
||||||
newDb = SingletonHolder.dbHelper.getReadableDatabase();
|
newDb = dbHelper.getReadableDatabase();
|
||||||
}
|
}
|
||||||
return newDb;
|
return newDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized PodDBAdapter open() {
|
||||||
|
// do nothing
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized void close() {
|
public synchronized void close() {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
@ -371,8 +369,8 @@ public class PodDBAdapter {
|
||||||
* <a href="https://github.com/robolectric/robolectric/issues/1890">robolectric/robolectric#1890</a>.</p>
|
* <a href="https://github.com/robolectric/robolectric/issues/1890">robolectric/robolectric#1890</a>.</p>
|
||||||
*/
|
*/
|
||||||
public static void tearDownTests() {
|
public static void tearDownTests() {
|
||||||
db = null;
|
getInstance().dbHelper.close();
|
||||||
SingletonHolder.dbHelper.close();
|
instance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean deleteDatabase() {
|
public static boolean deleteDatabase() {
|
||||||
|
@ -380,7 +378,7 @@ public class PodDBAdapter {
|
||||||
adapter.open();
|
adapter.open();
|
||||||
try {
|
try {
|
||||||
for (String tableName : ALL_TABLES) {
|
for (String tableName : ALL_TABLES) {
|
||||||
db.delete(tableName, "1", null);
|
adapter.db.delete(tableName, "1", null);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} finally {
|
} finally {
|
||||||
|
|
Loading…
Reference in New Issue