Transactions over different models

332 views
Skip to first unread message

Taylor Schwarzchild

unread,
Jan 28, 2016, 6:40:00 PM1/28/16
to LoopbackJS
This isn't explained in the loopback documentation but I want to use transactions over multiple models. I'm doing something similar in the following pseudo code where I create a transaction in one model and use it in other models. Will this work or am I forced to actually create a sperate transaction for each model?:

function getTx() {
        var defer = Q.defer();

        var tx = app.models.Hangout.beginTransaction({
            isolationLevel: Hangout.Transaction.READ_UNCOMMITED,
            timeout: 30000
        }, function (err, tx) {
            if (err) {
                defer.reject(err);
            }

            var options = { transaction: tx };

            tx.observe('timeout', function (context, next) {
                console.log('After timeout txCreate');
                next();
            });

            tx.observe('after commit', function (context, next) {
                console.log('After commit txCreate');
                next();
            });

            tx.observe('after rollback', function (context, next) {
                console.log('After rollback txCreate');
                next();
            });
            
            defer.resolve({transaction: tx});
        });

        return defer.promise;
    }

// Other code

Hangout.someMethod = function (...) {
   getTx().
   then(
       function (options) {
          app.models.SomeMode.someMethod(options,function(err,SomeModel) {
                      someModel.property = 1;
                      someMode.save(options, function (err) {
                            if (err) options.transaction.rollback();
                           app.models.SomeOtherModel.updateAll({... somefilter ..}, {...some data ...}, options, function (err) {
                                      if (err) options.transaction.rollback();
                                      // Save if we are successful
                                      options.transaction.commit();
                            });
                      });
          });
       }
   )

}

Raymond Feng

unread,
Jan 28, 2016, 7:27:09 PM1/28/16
to loopb...@googlegroups.com
As long as the models are attached to the same data source (DB), you can begin a transaction and pass it in options to multiple model method calls. The connector will use the same transaction context.

Thanks,
Raymond

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/dec9e9b1-ca71-4eae-97fc-479f757739ba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Taylor Schwarzchild

unread,
Jan 30, 2016, 11:34:38 PM1/30/16
to LoopbackJS
Thank You! It seemed me that what have said is the case, I just wanted to double check. Thanks Again :)

pookde...@gmail.com

unread,
Dec 1, 2018, 4:59:36 AM12/1/18
to loopb...@googlegroups.com
Hellou Raymond with the version 3 of loopback how can i use this example??
I try it but no rollback my data ..

try {
  await app.dataSources.db.transaction(async models => {
    const {MyModel} = models;
    console.log(await MyModel.count()); // 0
    await MyModel.create({foo: 'bar'});
    console.log(await MyModel.count()); // 1
    throw new Error('Oops');
  });
} catch (e) {
  console.log(e); // Oops
}
console.log(await app.models.MyModel.count()); // 0

this is my code:


try {
await app.dataSources.db.transaction(async models => {
console.log(await Grupos.count()); // 0
await Grupos.create(data);
console.log(await Grupos.count()); // 1
throw new Error('Oops');
});
} catch (e) {
console.log(e); // Oops
}
console.log(await Grupos.count()); // 0

Thanks !
Reply all
Reply to author
Forward
0 new messages