Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mahdiyari opened this issue Aug 23, 2024 · 0 comments
Open

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

mahdiyari opened this issue Aug 23, 2024 · 0 comments

Comments

@mahdiyari
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant