Sequelize 事务在错误抛出时不进行回滚

问题描述:

我正在使用 sequelize 事务(托管)并且当错误抛出回滚并没有真正发生时,也许我在这里做错了什么?

I am using sequelize transaction (managed) and when error throws rollback is not really happening, maybe i am doing something wrong here ?

我的测试用例:当代码块 2 抛出错误时,代码块 1 中所做的事情不会回滚,请帮忙.

my test case : when code block 2 throws an error the things done in code block 1 are not rolled back, please help.

return sequelize.transaction(function (t) {
        return db.VenueTag.destroy({where: {venueId: venue.venueId}}, {transaction: t}).then(function () {
        }, {transaction: t}).then(function () {
        //block 1
            return db.VenueTag.upsert({
            ...
            });
        }, {transaction: t}).then(function () {
        //block 2
            //update venue details
            return venue.updateAttributes({
                ...
            },{ transaction: t});
        });
    }).then(function (result) {
        // Transaction has been committed
        // result is whatever the result of the promise chain returned to the transaction callback
        return res.status(200).json(result);
    }).catch(function (err) {
        // Transaction has been rolled back
        // err is whatever rejected the promise chain returned to the transaction callback
        return res.status(500).send({
            message: errorHandler.getErrorMessage(err)
        });
    });

你的 Promise 链有错误,试试这个:

You had mistakes in your promise chain, try this:

return sequelize.transaction(function (t) {
    return db.VenueTag.destroy({where: {venueId: venue.venueId}}, {transaction: t})
    .then(function () {
    //block 1
        return db.VenueTag.upsert({
        ...
        }, {transaction: t});
    .then(function () {
    //block 2
        //update venue details
        return venue.updateAttributes({
            ...
        },{ transaction: t});
    });
}).then(function (result) {
    // Transaction has been committed
    // result is whatever the result of the promise chain returned to the transaction callback
    return res.status(200).json(result);
}).catch(function (err) {
    // Transaction has been rolled back
    // err is whatever rejected the promise chain returned to the transaction callback
    return res.status(500).send({
        message: errorHandler.getErrorMessage(err)
    });
});