Hussein Al-Far
Hussein Al-Far

Reputation: 719

Sequelize transaction Not doing rollback when error throws

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

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)
        });
    });

Upvotes: 1

Views: 15658

Answers (3)

Rishab Surana
Rishab Surana

Reputation: 2167

If you are using something like this below

return Users.update(updateUser, {
    where: {
        uid: sessionUser.uid,
        status: 'ACTIVE'
    },
    transaction: t     //second parameter is "options", so transaction must be in it
})

Then transaction is a key in the second options parameter, and not in third

Upvotes: 0

wattry
wattry

Reputation: 994

If you're catering to an ES7 audience or using node.js. You can use the async/await approach to simplify the callback/promise chain:

    /*Global Setup*/
    let venue, db; // Whatever instances these are....

    async function deleteVenueTag () {
      let transaction;
      try {
        transaction = await sequelize.transaction();
        let VenueTag = await db.findOne({where: {venueId: venue.venueId}}, {transaction});
        if (VenueTag) {
          await VenueTag.destroy({ transaction });
          await VenueTag.upsert({ transaction });
          await venue.updateAttributes({ transaction });
          await transaction.commit();
        }
      } catch (error) {
        transaction.rollback();
      }
    }

Upvotes: 0

Yuri Zarubin
Yuri Zarubin

Reputation: 11677

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)
    });
});

Upvotes: 3

Related Questions