This repository was archived by the owner on Aug 17, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed
androidTest/java/com/squareup/sqlbrite
main/java/com/squareup/sqlbrite Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change 3939import org .junit .runner .RunWith ;
4040import rx .Observable ;
4141import rx .Subscription ;
42+ import rx .functions .Action1 ;
4243import rx .functions .Func1 ;
4344import 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 ()
Original file line number Diff line number Diff 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 () {
You can’t perform that action at this time.
0 commit comments