我试图做一件非常简单的事情:
START TRANSACTION;
DELETE FROM table WHERE id = 1;
ROLLBACK;
在postgres
数据库上运行此功能非常有效。使用massive.js
时,它不会:
this.db.run(
"START TRANSACTION",
[]
);
setTimeout(() => {
this.db.run(
"DELETE FROM table WHERE id = $1"
[1]
);
}, 2000);
setTimeout(() => {
this.db.run(
"ROLLBACK;"
[]
);
}, 4000);
它不会回滚更改,只是从数据库中删除。 COMMIT
也不起作用。怎么了?
有没有办法转储查询订单?
答案 0 :(得分:0)
下方大量使用pg-promise,后者又支持交易:
db.instance.tx(t => {
// BEGIN has been executed
return t.none('DELETE FROM table WHERE id = $1', [123])
.then(() => {
// Records have been deleted within the transaction
throw new Error('Random error to fail the transaction');
// = the same as returning Promise.reject(new Error('Random...'))
});
})
.catch(error => {
// ROLLBACK has been executed, no records end up deleted.
console.log(error);
});
另见:
有没有办法转储查询订单?
Monitoring Queries向您展示了如何操作,或者在初始化db.driverConfig
时可以在massive
对象中添加事件query处理程序。