sequelize: Transaction commit failing to insert data

I am using transactions, all appears to succeed, but 90% of the time no data is committed to the table.

Within the transaction I am executing the following code:

Users.find({ activationCode : activationCode }, {transaction: transaction})
Users.create(objectToCreate, {transaction: transaction})

I am seeing the following in the logs:

Executing (d5316d08-d28a-4b12-94a4-026b80821f12): START TRANSACTION;
Executing (d5316d08-d28a-4b12-94a4-026b80821f12): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (d5316d08-d28a-4b12-94a4-026b80821f12): SET autocommit = 1;
Executing (default): SELECT * FROM "Users" WHERE "Users"."primaryEmailAddress"='test@mailhop.co.uk' LIMIT 1;
Executing (d5316d08-d28a-4b12-94a4-026b80821f12): INSERT INTO "Users" ("firstRun","active","createdAt","updatedAt","name","primaryEmailAddress","password","activationCode","resetPasswordCode") VALUES (true,false,'2014-02-19 06:03:20.702 +00:00','2014-02-19 06:03:20.704 +00:00','Test','test@mailhop.co.uk','sha1$572c4253$1$bbe0bb48e9e114daf48b7ada5a34c5cfed8f9250','5acc02b1-4799-4ed8-a58d-1f62ef54d119',NULL) RETURNING *;
Executing (d5316d08-d28a-4b12-94a4-026b80821f12): COMMIT;

I can’t see any reason, but I notice the find isn’t using the transaction. I am not sure if that is significant or note.

Any help would be much appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 54 (36 by maintainers)

Most upvoted comments

Thanks for the help. The ideal solution, for me, would be to ignore the transaction completely. For example:

sequelize.startTransaction(function () {
    doSomething
            .then(function (result) {
                sequelize.commit().success(function () {
                    ...
                });
            .fail(function (error) {
                transaction.rollback().success(function () {
                    ....
                });
            })
});

Then on “sequelize.startTransaction()”, if you used continuation-local-storage (https://www.npmjs.org/package/continuation-local-storage) you could save the transaction. For example:

require('continuation-local-storage').getNamespace('sequelize').set('transaction', transaction);

You would need to initialize the namespace once when Sequelize starts:

require('continuation-local-storage').createNamespace('sequelize');

Then when I call an operation (e.g. Users.find({ activationCode : activationCode })), you then pull the transaction and set it appropriately:

transaction = require('continuation-local-storage').getNamespace('sequelize').get('transaction');

As a developer all I am concerned about is demarcing the transaction boundary. I don’t need to care about how it is propagated and passed in to each function call.