在 GraphQL+Sequelize+CLS 中抛出错误后事务仍然自动提交
Transaction still autocommit after throw error in GraphQL+Sequelize+CLS
我想知道我是否在编码中有任何地方做错了。
我正在尝试执行一系列功能和检查,如果不满足某些条件,我想回滚事务。
我指的是
中的参考
我的事务在抛出新错误后仍然得到提交。
这是我的示例代码。
//db.js
import Sequelize from 'sequelize';
import Cls from 'continuation-local-storage';
Sequelize.cls = Cls.createNamespace('db');
const Conn = new Sequelize(
'relay',
'postgres',
'tec832',
{
dialect: 'postgres',
host: 'localhost'
}
);
module.exports.Db = Conn;
在 schema.js 我有
Db.transaction({autocommit: false}).then(()=>{
args = {vcTitle: {$ilike: `Sample title by Nick`}};
return testDelPostPromiseNoTran(args).then(obResult => {
throw new Error("Wrong account type");
})
});
function testDelPostPromiseNoTran(args){
return new Promise((resolve, reject) => {
resolve(delDataNoTran('Post', args));
});
}
//I am trying to do a standard delete
function delDataNoTran(vcTbName, args){
return Db.models[vcTbName].destroy({where: args})
.then(result =>{
if(result > 0){
return true;
}else{
return false;
}
})
.error(obStatus =>{
return false;
});
}
您的代码存在一些问题:
Db.transaction({autocommit: false}).then(()=>{
args = {vcTitle: {$ilike: `Sample title by Nick`}};
return testDelPostPromiseNoTran(args).then(obResult => {
throw new Error("Wrong account type");
})
});
应该...
Db.transaction(()=>{
args = {vcTitle: {$ilike: `Sample title by Nick`}};
return testDelPostPromiseNoTran(args).then(obResult => {
throw new Error("Wrong account type");
})
});
您应该将函数传递给 Db.transaction
。将它放在 .then()
块内意味着它在 之后 交易运行,因此不包含在交易中。这是你的主要问题。
function testDelPostPromiseNoTran(args){
return new Promise((resolve, reject) => {
resolve(delDataNoTran('Post', args));
});
}
... return 一个永远解决的承诺。这是多余的,因为 delDataNoTran
已经 return 是一个承诺——没有理由将它包装在另一个承诺中。
args = {vcTitle: {$ilike: `Sample title by Nick`}};
"ilike" 应该是 "iLike"(区分大小写。)
我建议您花一些时间阅读 Sequelize docs。
我想知道我是否在编码中有任何地方做错了。 我正在尝试执行一系列功能和检查,如果不满足某些条件,我想回滚事务。
我指的是
我的事务在抛出新错误后仍然得到提交。
这是我的示例代码。
//db.js
import Sequelize from 'sequelize';
import Cls from 'continuation-local-storage';
Sequelize.cls = Cls.createNamespace('db');
const Conn = new Sequelize(
'relay',
'postgres',
'tec832',
{
dialect: 'postgres',
host: 'localhost'
}
);
module.exports.Db = Conn;
在 schema.js 我有
Db.transaction({autocommit: false}).then(()=>{
args = {vcTitle: {$ilike: `Sample title by Nick`}};
return testDelPostPromiseNoTran(args).then(obResult => {
throw new Error("Wrong account type");
})
});
function testDelPostPromiseNoTran(args){
return new Promise((resolve, reject) => {
resolve(delDataNoTran('Post', args));
});
}
//I am trying to do a standard delete
function delDataNoTran(vcTbName, args){
return Db.models[vcTbName].destroy({where: args})
.then(result =>{
if(result > 0){
return true;
}else{
return false;
}
})
.error(obStatus =>{
return false;
});
}
您的代码存在一些问题:
Db.transaction({autocommit: false}).then(()=>{
args = {vcTitle: {$ilike: `Sample title by Nick`}};
return testDelPostPromiseNoTran(args).then(obResult => {
throw new Error("Wrong account type");
})
});
应该...
Db.transaction(()=>{
args = {vcTitle: {$ilike: `Sample title by Nick`}};
return testDelPostPromiseNoTran(args).then(obResult => {
throw new Error("Wrong account type");
})
});
您应该将函数传递给 Db.transaction
。将它放在 .then()
块内意味着它在 之后 交易运行,因此不包含在交易中。这是你的主要问题。
function testDelPostPromiseNoTran(args){
return new Promise((resolve, reject) => {
resolve(delDataNoTran('Post', args));
});
}
... return 一个永远解决的承诺。这是多余的,因为 delDataNoTran
已经 return 是一个承诺——没有理由将它包装在另一个承诺中。
args = {vcTitle: {$ilike: `Sample title by Nick`}};
"ilike" 应该是 "iLike"(区分大小写。)
我建议您花一些时间阅读 Sequelize docs。