续集更新事务
sequelize update transaction
我在 Nodejs 中使用 sequalize 事务,但我的问题是它没有在事务中使用我的 users
table 并更新我的 table
return sequelize.transaction(function (t) {
var Users = objAllTables.users.users();
return Users.update(updateUser, {
where: {
uid: sessionUser.uid,
status: 'ACTIVE'
}
},{ transaction: t }).then(function (result) {
return Utils.sendVerificationEmail(sessionUser.uid, sessionUser.user_email)
.then(function(data){
data = false;
if(data == false){
throw new Error('Failed Email');
}
});
}).then(function (result) {
console.log(result);
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
})
}).catch(function(err){
res.send({message:err.message})
})
控制台:
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): START TRANSACTION;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET autocommit = 1;
Executing (default): UPDATE `users` SET `username`='edited' WHERE `uid` = 20 AND `status` = 'ACTIVE'
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): ROLLBACK;
正如您在控制台更新查询中看到的那样运行出交易
transaction
密钥必须在 options
:
return Users.update(updateUser, {
where: {
uid: sessionUser.uid,
status: 'ACTIVE'
},
transaction: t //second parameter is "options", so transaction must be in it
})
我在 Nodejs 中使用 sequalize 事务,但我的问题是它没有在事务中使用我的 users
table 并更新我的 table
return sequelize.transaction(function (t) {
var Users = objAllTables.users.users();
return Users.update(updateUser, {
where: {
uid: sessionUser.uid,
status: 'ACTIVE'
}
},{ transaction: t }).then(function (result) {
return Utils.sendVerificationEmail(sessionUser.uid, sessionUser.user_email)
.then(function(data){
data = false;
if(data == false){
throw new Error('Failed Email');
}
});
}).then(function (result) {
console.log(result);
// Transaction has been committed
// result is whatever the result of the promise chain returned to the transaction callback
})
}).catch(function(err){
res.send({message:err.message})
})
控制台:
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): START TRANSACTION;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET autocommit = 1;
Executing (default): UPDATE `users` SET `username`='edited' WHERE `uid` = 20 AND `status` = 'ACTIVE'
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): ROLLBACK;
正如您在控制台更新查询中看到的那样运行出交易
transaction
密钥必须在 options
:
return Users.update(updateUser, {
where: {
uid: sessionUser.uid,
status: 'ACTIVE'
},
transaction: t //second parameter is "options", so transaction must be in it
})