question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transaction commit failing to insert data

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
p15martincommented, Feb 19, 2014

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.

0reactions
p15martincommented, Mar 7, 2014

@sdepold thanks for taking a look. I get the following error and a rollback (I need to scroll a way up to see the error):

{ [error: insert or update on table "EventParameters" violates foreign key constraint      "EventParameters_EventId_fkey"]
length: 278,
name: 'error',
severity: 'ERROR',
code: '23503',
detail: 'Key (EventId)=(807) is not present in table "Events".',
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
file: 'ri_triggers.c',
line: '3232',
routine: 'ri_ReportViolation',
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling transaction commit failures - EF6 - Microsoft Learn
Option 1 - Do nothing. The likelihood of a connection failure during transaction commit is low so it may be acceptable for your...
Read more >
Ensure MySQL transaction fails on COMMIT - Stack Overflow
You executed several inserts and before your commit your database server dies. A transaction may fail if it doesn't receive commit .
Read more >
Can we commit the transaction when the sql server trigger fails?
Even if the trigger fails, I want the data to be inserted in the Customers table, and the stored procedure should return the...
Read more >
can't commit transaction - webMethods
but i found out the transaction don't work. it call public.art.transaction:rollbackTransaction to roll back the transaction after insert by ...
Read more >
error: cannot commit (when inserting the row) — oracle-tech
Commits should be done outside and after the trigger has completed. If there is any reason to write to a table and commit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found