Skip to content

Commit 3ccd683

Browse files
authored
Catch IllegalStateException when ending the transaction (#1178)
* If endTransaction is called without an transaction IllegalStateException will be fired
1 parent 05b05de commit 3ccd683

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalDbHelper.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,19 @@ public void insert(@NonNull String table, @Nullable String nullColumnHack, @Null
206206
writableDb.beginTransaction();
207207
writableDb.insert(table, nullColumnHack, values);
208208
writableDb.setTransactionSuccessful();
209+
} catch (SQLiteException e) {
210+
logger.error("Error inserting on table: " + table + " with nullColumnHack: " + nullColumnHack + " and values: " + values, e);
211+
} catch (IllegalStateException e) {
212+
logger.error("Error under inserting transaction under table: " + table + " with nullColumnHack: " + nullColumnHack + " and values: " + values, e);
209213
} finally {
210-
try {
211-
writableDb.endTransaction(); // May throw if transaction was never opened or DB is full.
212-
} catch (SQLException e) {
213-
logger.error("Error closing transaction! ", e);
214+
if (writableDb != null) {
215+
try {
216+
writableDb.endTransaction(); // May throw if transaction was never opened or DB is full.
217+
} catch (IllegalStateException e) {
218+
logger.error("Error closing transaction! ", e);
219+
} catch (SQLiteException e) {
220+
logger.error("Error closing transaction! ", e);
221+
}
214222
}
215223
}
216224
}
@@ -225,14 +233,18 @@ public void insertOrThrow(@NonNull String table, @Nullable String nullColumnHack
225233
writableDb.beginTransaction();
226234
writableDb.insertOrThrow(table, nullColumnHack, values);
227235
writableDb.setTransactionSuccessful();
228-
} catch (Throwable t) {
229-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error inserting under table: " + table, t);
236+
} catch (SQLiteException e) {
237+
logger.error("Error inserting or throw on table: " + table + " with nullColumnHack: " + nullColumnHack + " and values: " + values, e);
238+
} catch (IllegalStateException e) {
239+
logger.error("Error under inserting or throw transaction under table: " + table + " with nullColumnHack: " + nullColumnHack + " and values: " + values, e);
230240
} finally {
231241
if (writableDb != null) {
232242
try {
233243
writableDb.endTransaction(); // May throw if transaction was never opened or DB is full.
234-
} catch (Throwable t) {
235-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error closing transaction! ", t);
244+
} catch (IllegalStateException e) {
245+
logger.error("Error closing transaction! ", e);
246+
} catch (SQLiteException e) {
247+
logger.error("Error closing transaction! ", e);
236248
}
237249
}
238250
}
@@ -242,20 +254,27 @@ public void insertOrThrow(@NonNull String table, @Nullable String nullColumnHack
242254
@Override
243255
public int update(@NonNull String table, @NonNull ContentValues values, @Nullable String whereClause, @Nullable String[] whereArgs) {
244256
int result = 0;
257+
if (values == null || values.toString().isEmpty())
258+
return result;
259+
245260
synchronized (LOCK) {
246261
SQLiteDatabase writableDb = getSQLiteDatabaseWithRetries();
247262
try {
248263
writableDb.beginTransaction();
249264
result = writableDb.update(table, values, whereClause, whereArgs);
250265
writableDb.setTransactionSuccessful();
251-
} catch (Throwable t) {
252-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error updating table: " + table, t);
266+
} catch (SQLiteException e) {
267+
logger.error("Error updating on table: " + table + " with whereClause: " + whereClause + " and whereArgs: " + whereArgs, e);
268+
} catch (IllegalStateException e) {
269+
logger.error("Error under update transaction under table: " + table + " with whereClause: " + whereClause + " and whereArgs: " + whereArgs, e);
253270
} finally {
254271
if (writableDb != null) {
255272
try {
256273
writableDb.endTransaction(); // May throw if transaction was never opened or DB is full.
257-
} catch (Throwable t) {
258-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Error closing transaction! ", t);
274+
} catch (IllegalStateException e) {
275+
logger.error("Error closing transaction! ", e);
276+
} catch (SQLiteException e) {
277+
logger.error("Error closing transaction! ", e);
259278
}
260279
}
261280
}
@@ -272,11 +291,15 @@ public void delete(@NonNull String table, @Nullable String whereClause, @Nullabl
272291
writableDb.delete(table, whereClause, whereArgs);
273292
writableDb.setTransactionSuccessful();
274293
} catch (SQLiteException e) {
275-
logger.error("Error deleting on table: " + table, e);
294+
logger.error("Error deleting on table: " + table + " with whereClause: " + whereClause + " and whereArgs: " + whereArgs, e);
295+
} catch (IllegalStateException e) {
296+
logger.error("Error under delete transaction under table: " + table + " with whereClause: " + whereClause + " and whereArgs: " + whereArgs, e);
276297
} finally {
277298
if (writableDb != null) {
278299
try {
279300
writableDb.endTransaction(); // May throw if transaction was never opened or DB is full.
301+
} catch (IllegalStateException e) {
302+
logger.error("Error closing transaction! ", e);
280303
} catch (SQLiteException e) {
281304
logger.error("Error closing transaction! ", e);
282305
}

0 commit comments

Comments
 (0)