【问题标题】:sequelize rollback isnot working when new error throw抛出新错误时,续集回滚不起作用
【发布时间】:2020-03-03 18:31:09
【问题描述】:

我正在尝试将三个表作为事务更新。如果其中一个表没有更新,我想回滚它。我尝试在 MySQL 数据库中手动执行查询,它工作正常。但是在代码中,它不能正常工作,不能回滚。

这里是代码,

return sequelize.transaction({
    autocommit: false
}, function(t) {
    return models.VaccinationCenter.update({
        email
    }, {
        where: {
            id: vacId
        }
    }, {
        transaction: t
    }).then(function(VaccinationCenter) {
        //console.log('---------------VaccinationCenter--------------------------------',VaccinationCenter)
        if (VaccinationCenter[0] === 0) {
            throw new Error();
            //console.log('VaccinationCenter--------------error')
        } else {
            return models.Person.update({
                    email
                }, {
                    where: {
                        email: prevEmail
                    }
                }, {
                    transaction: t
                })
                .then(function(Person) {
                    //console.log('---------------Person--------------------------------',Person);
                    if (Person[0] === 0) {
                        //console.log('Person--------------error');
                        throw new Error();
                    } else {
                        return models.User.update({
                                email
                            }, {
                                where: {
                                    email: prevEmail
                                }
                            }, {
                                transaction: t
                            })
                            .then(function(User) {
                                if (User[0] === 0) {
                                    //console.log('User--------------error');
                                    throw new Error();
                                } else {
                                    callback({
                                        statusCode: Constants.errorStatus.SUCCESS,
                                        body: {
                                            isValidemail: true
                                        }
                                    });
                                }
                                //console.log('---------------User--------------------------------',User)
                            });
                    }

                });
        }

    });

}).then(result => {
    callback({
        statusCode: Constants.errorStatus.SUCCESS,
        body: {
            isValidemail: true
        }
    });

}).catch(error => {
    callback({
        statusCode: Constants.errorStatus.BAD_REQUEST,
        body: {
            isValidEmail: false
        }
    });
});

这是运行此代码时的控制台。

Executing (f4d0d13f-d72e-4cb7-bd1b-c26e6ceddaca): START TRANSACTION;
Executing (f4d0d13f-d72e-4cb7-bd1b-c26e6ceddaca): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (f4d0d13f-d72e-4cb7-bd1b-c26e6ceddaca): SET autocommit = 0;
Executing (default): UPDATE `VaccinationCenters` SET `email`='devakadabare1+12@gmail.com',`updatedAt`='2020-03-03 10:06:14' WHERE `id` = 60
Executing (default): UPDATE `People` SET `email`='devakadabare1+12@gmail.com',`updatedAt`='2020-03-03 10:06:14' WHERE `email` = 'devakadabare1+11@gmail.com'
Executing (f4d0d13f-d72e-4cb7-bd1b-c26e6ceddaca): ROLLBACK;

【问题讨论】:

    标签: mysql node.js database sequelize.js rollback


    【解决方案1】:

    使用托管事务时,您永远不应手动提交或回滚事务。如果所有查询都成功,但您仍想回滚事务(例如由于验证失败),则应抛出错误以中断并拒绝链。例如:

    return sequelize.transaction(function (t) {
      return User.create({
        firstName: 'Abraham',
        lastName: 'Lincoln'
      }, {transaction: t}).then(function (user) {
        // Woops, the query was successful but we still want to roll back!
        throw new Error();
      });
    });
    

    更多详情,请查看documentation

    【讨论】:

    • where: { email: prevEmail }, transaction: t 替换这段代码where: { email: prevEmail } }, { transaction: t }就可以了
    猜你喜欢
    • 1970-01-01
    • 2017-01-14
    • 2015-11-02
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多