【问题标题】:Check status of sequelize transaction?检查sequelize交易的状态?
【发布时间】:2019-02-14 20:32:17
【问题描述】:

如何检查事务是否正在执行、提交或回滚?

return sequelize.transaction(function (t) { // return statements }).then(function (res_) { t.commit() }).catch( function (err) { t.rollback(); }); //Here I want to check the transaction status if(t.status != 'committed') { // transaction not committed } }

【问题讨论】:

    标签: mysql node.js sequelize.js


    【解决方案1】:

    您已经很接近了,但您的代码结构必须有所不同。您正在使用 Sequelize 中的自动事务。当回调函数完全完成时,它将自动为您提交事务。同样,当在任何时候抛出错误时,事务将自动取消。您可以使用以下方式检查事务是否已提交:

    return sequelize
        .transaction(function (t) {
            // Run some queries, do some stuff...
        })
        .then(function () {
            // `t` is not defined, but we know it is committed here
        })
        .catch(function (error) {
            // `t` is not defined, but we know it has been rolled back
        });
    

    如果您想依赖正在提交的事务,则必须将您的逻辑嵌套在 then 调用中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2015-09-12
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多