node-sqlserver-v8 icon indicating copy to clipboard operation
node-sqlserver-v8 copied to clipboard

How do I grab a connection from a pool for a transaction

Open TheThing opened this issue 9 months ago • 0 comments

Most the samples involving transactions use a singular connection.

I'm however using a pool.

My worry is following

let pool = new mssql.Pool(config)
pool.open()

/* later */
async function doSomething() {
  let promises = pool.promises

  try {
      await promises.query('BEGIN TRANSACTION')
      /* do select and locking and updating here */
      await promises.query('COMMIT')
    } catch (err) {
      await promises.query('ROLLBACK')
      throw err
    }
}

My problem is, the nature of pooling, I'm not sure all my queries will be running on the same connection. And if they aren't, then that's a recipe for disaster since transaction are connection-scoped.

Is there a way to grab a specific connection from a pool?

I couldn't find it in the api or the documentations.

TheThing avatar Feb 04 '25 05:02 TheThing