Skip to content

Commit ef7b10a

Browse files
committed
Ensure database is closed even if ending transaction fails
If endTransaction() throws an exception then previously close() would not be called. This adds another try/catch block that only ends the transaction if begin transaction successfully completes and closes the database consistently across multiple method calls that may raise exceptions
1 parent 0c168a7 commit ef7b10a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ private <E> List<E> requestAndStore(SQLiteOpenHelper helper,
8585
SQLiteDatabase db = helper.getWritableDatabase();
8686
try {
8787
db.beginTransaction();
88-
89-
persistableResource.store(db, items);
90-
91-
db.setTransactionSuccessful();
88+
try {
89+
persistableResource.store(db, items);
90+
db.setTransactionSuccessful();
91+
} finally {
92+
db.endTransaction();
93+
}
9294
} finally {
93-
db.endTransaction();
9495
db.close();
9596
}
9697
return items;

0 commit comments

Comments
 (0)