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

with_transaction with explicitly specified pool names #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lukyanov
Copy link

@lukyanov lukyanov commented Aug 27, 2017

pgapp assumes that pgapp:squery and pgapp:equery in the callback function in with_transaction must not contain PoolName if you want those queries to be executed inside the transaction. While this is appropriate in most cases, you may end up with unexpected behaviour when using multiple pool names in your app.

In these changes I aim for two goals:

  1. You should be able to specify pool name inside the callback of with_transaction. The behaviour should be that if the pool name in squery or equery is not specified or equal to the one with_transaction was called with, it is considered as queries within the transaction. If the pool name is different, then those queries are working with another pool and are not within the transaction.

    Example:

     pgapp:with_transaction(pool1, fun() ->
                                  pgapp:squery("update ..."),
                                  pgapp:squery(pool1, "update ..."),
                                  pgapp:squery(pool1, "delete from ..."),
                                  pgapp:squery(pool2, "update ...")
                            end).

    The last query (to pool2) is outside the transaction, but an error in the query still affects the transaction in pool1.

  2. If nested calls of with_transaction with the same pool name take place, they should be ignored as postgres does not support nested transactions anyway.

    Example:

    doing_stuff1() ->
        pgapp:with_transaction(pool1, fun() ->
                                     pgapp:squery("update ..."),
                                     doing_stuff2()
                                end).
    
    doing_stuff2() ->
        pgapp:with_transaction(pool1, fun() ->
                                     pgapp:squery("update ..."),
                                     pgapp:squery("update ..."),
                               end).

    While doing_stuff2 is a standalone function making queries in its own transaction, when being called in doing_stuff1 all the queries of both functions become a single transaction.

@lukyanov
Copy link
Author

An alternative for #23

@lukyanov lukyanov force-pushed the nested-transactions branch from 8a82880 to 8d905e0 Compare August 27, 2017 08:00
@lukyanov lukyanov changed the title Ignoring nested transactions with_transaction with explicitly specified pool names Aug 27, 2017
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.

1 participant