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

Added .transaction(options ?,asyncFunction) to PromisePool & PromiseConnection #811

Closed
wants to merge 8 commits into from

Conversation

mdierolf
Copy link

This PR adds a .withConnection(asyncFunction) function to PromisePool, which allows the user to borrow a connection from the connection pool, run as many queries as they like, and then it will automatically release the connection back to the pool when the asyncFunction(con) they pass in either resolves or rejects.

Solves a garbage disposal problem when using promise pools.

@sidorares
Copy link
Owner

sidorares commented Jul 25, 2018

let me think about this :) I'm trying to resist adding more functionality to pool but it actually looks useful

Couple of notes:
result of the function should be passed outside when inner promise resolved:

   const xyz = await promise.withConnection(async conn => {
     await conn.beginTransaction();
     const [rows] = await conn.query('insert .,,,');
     await conn.commit();
     return rows[0].someAutoIncrementField;
   });

also - maybe "withTransaction" that does .rollback() + .release in catch block? For individual unrelated queries there is no benefit of withConnection over await pool.query('q1'); await pool.query('q2') - pool.query and pool.execute also automatically release connection when done or error

Definitely needs unit tests to move forward

@mdierolf
Copy link
Author

Perhaps this would be better to model after the .transaction() call in the sequelize module.

http://docs.sequelizejs.com/manual/tutorial/transactions.html

A .transaction() with no isolation, in auto commit mode, with no rollback, would essentially have the same behavior as .withConnection(), except the developer would have to choose that mode explicitly.

It’s probably a good thing to force the developer to choose an isolation level.

@mdierolf
Copy link
Author

I changed .withConnection() to .transaction(), and added behavior to run queries under different types of transactions.

I haven't tried this code yet, it is completely untested.

The previous behavior of .withConnection() is now available by calling .transaction({ autoCommit: true })

@mdierolf mdierolf changed the title Added a .withConnection(asyncFunction) to PromisePool Added .transaction(options ?,asyncFunction) to PromisePool Jul 25, 2018
@mdierolf mdierolf changed the title Added .transaction(options ?,asyncFunction) to PromisePool Added .transaction(options ?,asyncFunction) to PromisePool & PromiseConnection Jul 25, 2018
@sidorares
Copy link
Owner

Could you extract ping() and Query.then() changes into separate pr? I'll merge those changes straight away.

When new api methods I'd like PRs to stay open a bit longer with more eyes, once it's in it's really difficult to go back without potentially breaking someone's code

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