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

pool: Add transaction helpers and support #354

Merged
merged 1 commit into from
Feb 9, 2025

Conversation

TheThing
Copy link
Contributor

@TheThing TheThing commented Feb 6, 2025

This adds some much needed support for doing transactions from a pool with a few helper utilities.
Notable new API methods:

pool.beginTransaction(function (err, description) { /* description.connection.query() */ })

Pulls a connection information out of the pool and gives it to the caller. This connection becomes busy for the duration until either of next two functions are called:

pool.commitTransaction(description, function(err) { /* err logging */ })

Calls IF (@@TRANCOUNT > 0) COMMIT TRANSACTION on the connection in the description and releases it back into the pool allowing it to be used by others.

pool.rollbackTransaction(description, function(err) { /* err logging */ })

Calls IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION on the connection in the description and releases it back into the pool allowing it to be used by others.

Both functions are also available in the promised pool:

let description = await pool.promises.beginTransaction()
try {
  await description.connection.promises.query(`
    SELECT * from my_table WITH (updlock, holdlock)
  `)
  // do some other stuff
  await pool.promises.commitTransaction(description)
} catch (err) {
  await pool.promises.rollbackTransaction(description)
  // log err
}

Lastly, a nice little promise-specific utlity has been added:

pool.promises.transaction(async function (description) {
  /* I'm inside a transaction */
  await description.connection.promises.query(`Do transaction query here`)
})

This calls the callback and wraps it inside of a transaction with auto commit on success and auto rollback on any thrown errors.

@TheThing
Copy link
Contributor Author

TheThing commented Feb 6, 2025

This closes #353

@TimelordUK
Copy link
Owner

this looks great, will hopefully pull over weekend - thanks a lot.

@TheThing
Copy link
Contributor Author

TheThing commented Feb 8, 2025

Thank you, I wanted to add unit tests for all this functionality but I couldn't get them to run. After npm install it complained it couldn't find const sql = require('msnodesqlv8') module in env/CommonTestFunctions and I didn't know how to fix that :(

Otherwise I would have added unit tests for all of these

@TimelordUK TimelordUK merged commit be43ddf into TimelordUK:master Feb 9, 2025
1 check passed
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

Successfully merging this pull request may close these issues.

2 participants