Sequelize 自动提交事务与非自动提交事务:

自动提交:

return sequelize.transaction(function (t) {

  // chain all your queries here. make sure you return them.
  return User.create({
    firstName: 'Abraham',
    lastName: 'Lincoln'
  }, {transaction: t}).then(function (user) {
    return user.setShooter({
      firstName: 'John',
      lastName: 'Boothe'
    }, {transaction: t});
  });

}).then(function (result) {
  // Transaction has been committed
  // result is whatever the result of the promise chain returned to the transaction callback
}).catch(function (err) {
  // Transaction has been rolled back
  // err is whatever rejected the promise chain returned to the transaction callback
});

非自动提交:

return sequelize.transaction().then(function (t) {
    return User.create({
        firstName: 'Bart',
        lastName: 'Simpson'
    }, {transaction: t}).then(function (user) {
        return user.addSibling({
            firstName: 'Lisa',
            lastName: 'Simpson'
        }, {transaction: t});
    }).then(function () {
        return t.commit();
    }).catch(function (err) {
        return t.rollback();
    });
});

猜你喜欢

转载自blog.csdn.net/FATii/article/details/81114993
今日推荐