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.

Confusion about transaction code:

See original GitHub issue

My transaction code:

try {
    const cn = await pool.getConnection()
    await cn.beginTransaction()

    await cn.execute(' ...
    await cn.execute(' ...

    await cn.commit()
}
catch {
    await cn.rollback()
}

But am I wrong? Should it be:

const cn = await pool.getConnection()
try {
    await cn.beginTransaction()

    await cn.execute(' ...
    await cn.execute(' ...

    await cn.commit()
}
catch {
    await cn.rollback()
}

But wait! Should it be?

const cn = await pool.getConnection()
await cn.beginTransaction()
try {
    await cn.execute(' ...
    await cn.execute(' ...

    await cn.commit()
}
catch {
    await cn.rollback()
}

I’m afraid I don’t know the answer. Thanks in advance anyone who can advise. (It may help others also?)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
sidorarescommented, Aug 30, 2022

there was a discussion somewhere here about possible better helper api that would handle this pattern, for example something similar to https://github.com/porsager/postgres#transactions ( I’ve started some initial work here )

2reactions
sidorarescommented, Aug 30, 2022

beginTransaction / commit / rollback are just simple helpers to perform START TRANSACTION / COMMIT / ROLLBACK queries

To me 3rd example is the most correct but I’m not an SQL / mysql expert

Possible reasons why pool.getConnection() can fail: network error in the process of getting a new connection, too many connections limit reached (server side), client side limit on number of open connections

Not sure when START TRANSACTION can fail ( reasons other than connection is killed because of network error or other client KILL thread_id ) but I guess rollback only makes sense after successful START TRANSACTION

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chargeback Reason Codes: The Ultimate Guide
However, the process can still cause a lot of confusion, especially at the merchant level, where there's no insight into the process of...
Read more >
Confusion in transactions · Issue #97 · simolus3/drift - GitHub
We call the transaction callback in a new Zone . Whenever a method is called on the database class, we will check if...
Read more >
Confusion about finding customer exit - SAP Community
I am sorry but I have a trivial issue that I am confused about. ... I came across a requirement to find customer...
Read more >
Transactions and Isolation Level Confusion - Ask TOM
Hi, Tom. I have some questions about transactions and isolation levels that come from my reading of your books and answers in this...
Read more >
SAP PRESS - T-codes can be confusing. Cut through the...
T-codes can be confusing. Cut through the confusion with our reference guide to transaction codes in SAP ERP. https://hubs.la/H0CqGz60.
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