Skip to content

Commit d4e6b89

Browse files
committed
Don't read/write to database if unavailable
Catch any SQLiteException thrown when a readable or writable database is requested from the SQLiteOpenHelper and don't attempt to store or read the items being cached. A few exceptions were reported where a SQLiteException was being raised and when uncaught it would cause the application to crash.
1 parent 8b896d6 commit d4e6b89

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

app/src/main/java/com/github/mobile/persistence/DatabaseCache.java

+53-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.database.Cursor;
1919
import android.database.sqlite.SQLiteDatabase;
20+
import android.database.sqlite.SQLiteException;
2021
import android.database.sqlite.SQLiteOpenHelper;
2122
import android.util.Log;
2223

@@ -38,6 +39,44 @@ public class DatabaseCache {
3839
@Inject
3940
private Provider<CacheHelper> helperProvider;
4041

42+
/**
43+
* Get writable database
44+
*
45+
* @param helper
46+
* @return writable database or null if it failed to create/open
47+
*/
48+
protected SQLiteDatabase getWritable(SQLiteOpenHelper helper) {
49+
try {
50+
return helper.getWritableDatabase();
51+
} catch (SQLiteException e1) {
52+
// Make second attempt
53+
try {
54+
return helper.getWritableDatabase();
55+
} catch (SQLiteException e2) {
56+
return null;
57+
}
58+
}
59+
}
60+
61+
/**
62+
* Get readable database
63+
*
64+
* @param helper
65+
* @return readable database or null if it failed to create/open
66+
*/
67+
protected SQLiteDatabase getReadable(SQLiteOpenHelper helper) {
68+
try {
69+
return helper.getReadableDatabase();
70+
} catch (SQLiteException e1) {
71+
// Make second attempt
72+
try {
73+
return helper.getReadableDatabase();
74+
} catch (SQLiteException e2) {
75+
return null;
76+
}
77+
}
78+
}
79+
4180
/**
4281
* Load or request given resources
4382
*
@@ -78,11 +117,15 @@ public <E> List<E> requestAndStore(
78117
}
79118
}
80119

81-
private <E> List<E> requestAndStore(SQLiteOpenHelper helper,
82-
PersistableResource<E> persistableResource) throws IOException {
83-
List<E> items = persistableResource.request();
120+
private <E> List<E> requestAndStore(final SQLiteOpenHelper helper,
121+
final PersistableResource<E> persistableResource)
122+
throws IOException {
123+
final List<E> items = persistableResource.request();
124+
125+
final SQLiteDatabase db = getWritable(helper);
126+
if (db == null)
127+
return items;
84128

85-
SQLiteDatabase db = helper.getWritableDatabase();
86129
try {
87130
db.beginTransaction();
88131
try {
@@ -97,9 +140,12 @@ private <E> List<E> requestAndStore(SQLiteOpenHelper helper,
97140
return items;
98141
}
99142

100-
private <E> List<E> loadFromDB(SQLiteOpenHelper helper,
101-
PersistableResource<E> persistableResource) {
102-
SQLiteDatabase db = helper.getReadableDatabase();
143+
private <E> List<E> loadFromDB(final SQLiteOpenHelper helper,
144+
final PersistableResource<E> persistableResource) {
145+
final SQLiteDatabase db = getReadable(helper);
146+
if (db == null)
147+
return null;
148+
103149
try {
104150
Cursor cursor = persistableResource.getCursor(db);
105151
try {

0 commit comments

Comments
 (0)