Skip to content

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

@mahdiyari

Description

@mahdiyari

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions