Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 47c20a2

Browse files
Alec StrongAlec Strong
authored andcommitted
Merge pull request #62 from square/jw/trigger-after-commit
Trigger notifications after commit.
2 parents 94789b9 + 7a68a06 commit 47c20a2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

sqlbrite/src/androidTest/java/com/squareup/sqlbrite/BriteDatabaseTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.junit.runner.RunWith;
4040
import rx.Observable;
4141
import rx.Subscription;
42+
import rx.functions.Action1;
4243
import rx.functions.Func1;
4344
import rx.observables.BlockingObservable;
4445

@@ -503,6 +504,27 @@ public final class BriteDatabaseTest {
503504
.isExhausted();
504505
}
505506

507+
@Test public void transactionCreatedFromTransactionNotificationWorks() {
508+
// Tests the case where a transaction is created in the subscriber to a query which gets
509+
// notified as the result of another transaction being committed. With improper ordering, this
510+
// can result in creating a new transaction before the old is committed on the underlying DB.
511+
512+
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
513+
.subscribe(new Action1<Query>() {
514+
@Override public void call(Query query) {
515+
db.newTransaction().end();
516+
}
517+
});
518+
519+
Transaction transaction = db.newTransaction();
520+
try {
521+
db.insert(TABLE_EMPLOYEE, employee("john", "John Johnson"));
522+
transaction.markSuccessful();
523+
} finally {
524+
transaction.end();
525+
}
526+
}
527+
506528
@Test public void transactionIsCloseable() throws IOException {
507529
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
508530
o.assertCursor()

sqlbrite/src/main/java/com/squareup/sqlbrite/BriteDatabase.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public final class BriteDatabase implements Closeable {
8383
transactions.set(newTransaction);
8484
if (logging) log("TXN END %s", transaction);
8585
getWriteableDatabase().endTransaction();
86+
// Send the triggers after ending the transaction in the DB.
87+
if (transaction.commit) {
88+
sendTableTrigger(transaction.triggers);
89+
}
8690
}
8791

8892
@Override public void close() {
@@ -517,9 +521,10 @@ private static String conflictString(@ConflictAlgorithm int conflictAlgorithm) {
517521
}
518522
}
519523

520-
private final class SqliteTransaction implements SQLiteTransactionListener {
524+
private static final class SqliteTransaction implements SQLiteTransactionListener {
521525
final SqliteTransaction parent;
522526
final Set<String> triggers = new LinkedHashSet<>();
527+
boolean commit;
523528

524529
SqliteTransaction(SqliteTransaction parent) {
525530
this.parent = parent;
@@ -529,7 +534,7 @@ private final class SqliteTransaction implements SQLiteTransactionListener {
529534
}
530535

531536
@Override public void onCommit() {
532-
sendTableTrigger(triggers);
537+
commit = true;
533538
}
534539

535540
@Override public void onRollback() {

0 commit comments

Comments
 (0)