0

我正在尝试实现一个非常简单的事情:

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也不行。怎么了?

有什么方法可以转储查询顺序吗?

4

1 回答 1

0

Massive 在下面使用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);
    });

也可以看看:

有什么方法可以转储查询顺序吗?

监控查询向您展示了如何做到这一点,或者您可以在初始化时在对象中添加事件查询处理程序。db.driverConfigmassive

于 2017-08-16T04:19:21.050 回答