Skip to content

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

@TheThing

Description

@TheThing

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions