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.

I cant rollback inside a transaction

See original GitHub issue
const client = await pool.connect()
try {
  await client.query('begin')
  (...)
  if (condition) {
    await client.query('rollback') //does not work
  }
  (...) //queries here are still being committed
  await client.query('commit')
}
catch (e) {
  await client.query('rollback')
}
finally {
  client.release()
}

How do I force rollback inside a transaction if a condition matches?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
charmandercommented, Mar 21, 2019

ROLLBACK ends your transaction. Statements continue to work after a transaction like they do before a transaction. COMMIT does nothing outside of a transaction (https://www.postgresql.org/docs/current/sql-commit.html#id-1.9.3.53.7), but even if it were an error, the queries before it would take effect.

1reaction
joelmukuthucommented, Mar 21, 2019

@gustavopiucco, you need to return to halt execution, or add an else to the if statement

Read more comments on GitHub >

github_iconTop Results From Across the Web

ROLLBACK TRANSACTION (Transact-SQL) - SQL Server
Rolls back an explicit or implicit transaction to the beginning of the transaction, or to a savepoint inside the transaction. You can use ......
Read more >
Cannot rollback in if block transaction sql - Stack Overflow
When I executed this stored procedure, I cannot rollback in IF block even the IF statement was right. If the IF statement was...
Read more >
How to rollback using explicit SQL Server transactions
This demonstration shows that an explicit transaction rollbacks a transaction, but it cannot revert the identity value. It is the reason we see ......
Read more >
Can't rollback transaction - Forums - SQLServerCentral
The rollback itself will definitely take much longer than the initial transaction. It will require additional log space as well. If the ...
Read more >
Transactions - Snowflake Documentation
Statements inside an explicit transaction are not affected by AUTOCOMMIT. For example, statements inside an explicit BEGIN TRANSACTION ... ROLLBACK are rolled ...
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