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();
});
});
});
}
)
}