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.

Transactions - commit manual managed transaction

See original GitHub issue

What you are doing?

I want to create a transaction and control which queries it will run. I want to do that using node 7.8.0 and async/await syntax. Here is a sample of the code I’m trying to run -

const transaction = await db.sequelize.transaction();
await db.User.create({ username: 'roy' }, { transaction });
await transaction.commit();

What do you expect to happen?

The transaction to complete

What is actually happening?

I get this error-

commit has been called on this transaction(3b1ab60a-172c-4795-9ace-b89b855c92ce), you can no longer use it. (The rejected query is attached as the 'sql' property of this error)

btw, if I run my code without using transactions it works. I just want to understand how to use transactions the right way using async/await.

Thanks.

Dialect: postgres Sequelize version: 4.0.0-2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:9
  • Comments:9

github_iconTop GitHub Comments

26reactions
kfrajtakcommented, Oct 1, 2019

In my case the problem was a stupid mistake - missing await when calling the save

await entity.save({ transaction });
12reactions
dkushnercommented, Apr 18, 2017

This feature is desperately needed for code quality purposes and to ease the pain of testing with Sequelize at the moment.

@roytz, from everything I could find, the prescribed way to use transactions is to perform all of the operations you need to do from within the context of the next handler in the promise chain like so:

await db.sequelize.transaction().then(async transaction => {
    await db.User.create({ username: 'roy' }, { transaction })
})

Which I find fairly annoying to deal with.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - How to manually commit a managed transaction
Overall, I would say that it's usually a good idea to let the container manage your transactions, as a transaction is usually in...
Read more >
Container-Managed Transactions - The Java EE 6 Tutorial
In an enterprise bean with container-managed transaction demarcation, the EJB container sets the boundaries of the transactions.
Read more >
Transactions - Sequelize
Managed transactions handle committing or rolling back the transaction automatically. You start a managed transaction by passing a callback to sequelize.
Read more >
COMMIT TRANSACTION (Transact-SQL) - SQL Server
When used in nested transactions, commits of the inner transactions don't free resources or make their modifications permanent. The data ...
Read more >
Transactions and auto-commit - Manual - PHP
Auto-commit mode means that every query that you run has its own implicit transaction, if the database supports it, or no transaction if...
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