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.

Using nested transactions with save points

See original GitHub issue

Environment

Knex version: 0.19.1 Database + version: PostgreSQL 11 OS: MacOS Mojave

Bug

I am trying to use nested transactions with save points and it doesn’t seems to be working with promises API.

Here’s some code (I am sharing the minimum required code to reproduce the problem)

Following doesn’t work

async function run () {
  const trx = await knex.transaction()
  const trx2 = await trx.transaction()
  console.log('never reached here')
}

Following works

function runWithCallbacks () {
  knex.transaction((trx) => {
   trx.transaction((trx2) => {
    console.log('reached here>')
   })
 })
}

The first example doesn’t work since the promise doesn’t resolve unless trx2 commit/rollback and it cannot, since the code never moves to the next line.

Is this the intended behavior?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
kibertoadcommented, Aug 17, 2019

@thetutlage Released in 0.19.2

1reaction
thetutlagecommented, Aug 14, 2019

Found one regression here. Now, since we are faking the container, the rejection behavior of rollback is to always reject.

As a solution, I believe we can set doNotRejectOnRollback when it’s not explicitly defined when starting a nested transaction.

if (!options) {
	options = { doNotRejectOnRollback: true }
} else if (isUndefined(options.doNotRejectOnRollback)) {
	options.doNotRejectOnRollback = true
}

// The existing code
return new Promise((resolve, _reject) => {
	trxClient.transaction(
  	(nestedTrx) => {
    	resolve(nestedTrx);
  	},
  	options,
  	trx
	);
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested Transactions in SQL Server - Dot Net Tutorials
The SAVE TRANSACTION in SQL Server is used for dividing (or) breaking a transaction into multiple units so that the user has a...
Read more >
Savepoint | Transactions | Ebean
Ebean supports using Savepoints which are a feature of most Relational Databases in which we can create a Savepoint (nested transaction) inside a ......
Read more >
Use savepoints for nested transactions [#669794] | Drupal.org
Basically, we would create a new named savepoint with each nested "transaction." On each nested "commit," we tell the DB to retire the...
Read more >
Savepoints - SQLite
SAVEPOINTs are a method of creating transactions, similar to BEGIN ... that the SAVEPOINT and RELEASE commands are named and may be nested....
Read more >
SAVEPOINT | CockroachDB Docs
Transactions can be nested using named savepoints. RELEASE SAVEPOINT and ROLLBACK TO SAVEPOINT can both refer to a savepoint "higher" in the nesting...
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