trx.commit() does not throw error when commit fails
See original GitHub issueEnvironment
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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks, guys! That helps a lot. I have missed that while reading the docs.