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.

trx.commit() does not throw error when commit fails

See original GitHub issue

Environment

Knex version: 0.21.1 Database + version: PostgreSQL 12.2 OS: Ubuntu 18.04

Bug

When calling the commit() method on a transaction and the transaction fails because it was timed out by PostgreSQL through the idle_in_transaction_session_timeout setting, then no exception is thrown. Instead, the commit() method just returns undefined instead of an object.

Here’s a code example that shows the issue:

const sleep = async (ms) => new Promise((res) => setTimeout(() => res(), ms));

(async () => {
  // Set `idle_in_transaction_session_timeout` to 1000 ms.
  // Postgres will kill any transactions that are idle for longer than 1000 ms.
  await knex.raw('set idle_in_transaction_session_timeout to 1000;');

  // Create the transaction.
  const trx = await knex.transaction();

  // Execute any queries, doesn't really matter.
  const updated = await trx('users')
    .where('id', 1)
    .update({
      username: 'test1',
    });

  // We have to wait longer than 1000 ms for
  // `idle_in_transaction_session_timeout` to take effect.
  await sleep(1500);

  // Commit the transaction.
  // This will not throw an error. Instead, it will just return `undefined`.
  const commit = await trx.commit();

  // `isCompleted` also returns `true`,
  // even though the transaction was not really committed.
  const isCompleted = trx.isCompleted();

  // Get the previously updated row from the database to verify
  // that the transaction was not committed.
  const result = await knex('users').where('id', 1);

  console.log({
    updated,
    commit,
    result,
    isCompleted,
  });
})();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
elhigucommented, May 10, 2020
try {
  trx.commit(); // no need to await this since trx.executionPromise will wait it to finish anyways
  await trx. executionPromise;
  // transaction was success
} catch (err) {
  // transaction failed / was rolled back
}
0reactions
chescoscommented, May 10, 2020

Thanks, guys! That helps a lot. I have missed that while reading the docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to catch knex transaction rejection
Are you sure that there's a rollback to be done? I'm thinking that the transaction never committed b/c it threw an error before...
Read more >
Transactions
Throwing an error directly from the transaction handler function automatically rolls back the transaction, same as returning a rejected promise. Notice that if ......
Read more >
Handling Exceptions in a Transacted Session
A transacted session might fail to commit and (throw an exception) either because a failover occurs while statements within the transaction are being...
Read more >
An Interpretation of PolarDB-X Source Codes (10)
When a distributed transaction is committed, various situations may fail the commit. For example, all branch transactions are prepared and the transaction log ......
Read more >
Transactions
return nil will commit the whole transaction ... GORM supports nested transactions, you can rollback a subset of ... return errors.
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