denodrivers / denodrivers/postgres

How would two clients from a pool use the same connection?

Open
#486 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
655
Forks
97
PR merge metrics
No merged PRs in 30d

Description

Let me explain by code:
```
const testA = async (bigArray) => {
using client = await pool.connect()
const trx = client.createTransaction('testa')
await trx.begin()
for (let i = 0; i < bigArray.length; i++) {
await trx.queryObject('update some columns here', [params])
}
await trx.commit()
}

const testB = async () => {
using client = await pool.connect()
await client.queryObject('update some columns here on the same table')
}
setInterval(testB, 3000)
await testA(bigArray)
```
Basically we have an interval that runs a certain update query and it runs concurrently with the testA function as that function can take minutes to complete.

So what is the problem? When testB runs, it uses the same connection as the testA function and breaks everything. At first the error I was getting was from the function testA saying the transaction is not started yet. As it was looping in the middle of loop it would throw that error.
After some hours of testing I found out the error from the function testB that the connection is already at use by the transaction `testa`.
Neither of the functions are problematic on their own.
They are updating different set of columns so a deadlock is rare if any but I wasn't expecting the two clients to share the same connection.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.