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.

Rolling back a transaction without an error

See original GitHub issue

I’m using transacting with knex and I want to rollback the transaction during normal execution. But whenever transaction.rollback() is called it throws an unhandled exception. Providing a custom error object and trying to catch it doesn’t help since it is inside a callback.

Is there a way to rollback a transaction without throwing an error? Or is there a way to handle that error without littering the output?

Sample code:

// some previous inserts using `trans` done by now
db('actions')
.transacting(trans)
.select()
.where('id', data)
.then(function(rows) {
  // do some stuff with rows
  // we don't need the data anymore and want to roll back the transaction
  trans.rollback();
});

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
aj0strowcommented, Mar 30, 2016

It’s actually really hard to get unit tests with rollback working. I expected this to work:

beforeEach(function () {
  this.tx = knex.transaction()
})

afterEach(function () {
  this.tx.rollback()
})

What i actually had to do was …

Prevent connection pooling

var knex = require("knex")

var config = {
  client: "pg",
  connection: process.env["DATABASE_URL"]
}

var env = process.env["NODE_ENV"]

if (env == "test") {
  config.pool = {
    min: 1,
    max: 1,
  }
}

module.exports = knex(config)

Raw transaction in tests

beforeEach(function () {
  return knex.raw("BEGIN")
})

afterEach(function () {
  return knex.raw("ROLLBACK")
})

it("should do whatever", function () {
  var tx = knex
  return CallAction(tx, { options: true })
  .then(function (result) {
    assert(result)
  })
})
1reaction
elhigucommented, Jun 28, 2018

@rapodaca rolling back changes like that is really limited way to do testing and prevent you from testing many real world use-cases. Of course some specific cases that approach is legit. Just for your information and anyone else checking this issue out 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server - transactions roll back on error? - Stack Overflow
When a connection times out, the underlying network protocol (e. g. Named Pipes or TCP ) breaks the connection. When a connection is...
Read more >
How to rollback using explicit SQL Server transactions
In this article, we discussed implicit and explicit SQL Server transactions. We can decide to commit or rollback a transaction in explicit mode....
Read more >
ROLLBACK TRANSACTION (Transact-SQL) - SQL Server
Error Handling. A ROLLBACK TRANSACTION statement does not produce any messages to the user. If warnings are needed in stored procedures or ...
Read more >
SQL Server - Rollback Transaction Completely In Case Of Error
I recently found an issue while inserting data into a table during transactions that transaction will not completely roll back if we use ......
Read more >
Rollback Transaction On Error In SQL Server Stored Procedure
In any transaction scenario, we have to rollback transaction on error in SQL execution. Using TRY-CATCH we can capture the exception occurred in ......
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