Fix threading issue in RecipientDatabase.

This commit is contained in:
Greyson Parrelli 2019-10-04 12:07:46 -04:00
parent acea24c19c
commit 5f993ed0f7

View File

@ -188,18 +188,24 @@ public class RecipientDatabase extends Database {
String query = PHONE + " = ?"; String query = PHONE + " = ?";
String[] args = new String[] { e164 }; String[] args = new String[] { e164 };
synchronized (this) { db.beginTransaction();
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) { try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))); if (cursor != null && cursor.moveToFirst()) {
} else { db.setTransactionSuccessful();
ContentValues values = new ContentValues(); return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
values.put(PHONE, e164); } else {
long id = db.insertOrThrow(TABLE_NAME, null, values); ContentValues values = new ContentValues();
if (id < 0) throw new AssertionError("Failed to insert recipient!"); values.put(PHONE, e164);
return RecipientId.from(id);
} long id = db.insertOrThrow(TABLE_NAME, null, values);
if (id < 0) throw new AssertionError("Failed to insert recipient!");
db.setTransactionSuccessful();
return RecipientId.from(id);
} }
} finally {
db.endTransaction();
} }
} }
@ -212,18 +218,24 @@ public class RecipientDatabase extends Database {
String query = EMAIL + " = ?"; String query = EMAIL + " = ?";
String[] args = new String[] { email }; String[] args = new String[] { email };
synchronized (this) { db.beginTransaction();
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) { try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))); if (cursor != null && cursor.moveToFirst()) {
} else { db.setTransactionSuccessful();
ContentValues values = new ContentValues(); return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
values.put(EMAIL, email); } else {
long id = db.insertOrThrow(TABLE_NAME, null, values); ContentValues values = new ContentValues();
if (id < 0) throw new AssertionError("Failed to insert recipient!"); values.put(EMAIL, email);
return RecipientId.from(id);
} long id = db.insertOrThrow(TABLE_NAME, null, values);
if (id < 0) throw new AssertionError("Failed to insert recipient!");
db.setTransactionSuccessful();
return RecipientId.from(id);
} }
} finally {
db.endTransaction();
} }
} }
@ -236,18 +248,24 @@ public class RecipientDatabase extends Database {
String query = GROUP_ID + " = ?"; String query = GROUP_ID + " = ?";
String[] args = new String[] { groupId }; String[] args = new String[] { groupId };
synchronized (this) { db.beginTransaction();
try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) { try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) {
return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))); if (cursor != null && cursor.moveToFirst()) {
} else { db.setTransactionSuccessful();
ContentValues values = new ContentValues(); return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
values.put(GROUP_ID, groupId); } else {
long id = db.insertOrThrow(TABLE_NAME, null, values); ContentValues values = new ContentValues();
if (id < 0) throw new AssertionError("Failed to insert recipient!"); values.put(GROUP_ID, groupId);
return RecipientId.from(id);
} long id = db.insertOrThrow(TABLE_NAME, null, values);
if (id < 0) throw new AssertionError("Failed to insert recipient!");
db.setTransactionSuccessful();
return RecipientId.from(id);
} }
} finally {
db.endTransaction();
} }
} }