Hacker News new | ask | show | jobs
by slownews45 1782 days ago
The example syntax for connection handling is perfect. The footgun of not releasing the connection (especially from pools at the end of the with) is real. And the concepts are different (block of transactions vs normal with file xxx. )

  with connect(DSN) as conn:
    with conn.transaction():
        do_something()
        with conn.transaction():
            do_something_nested()

    with conn.transaction() as tx:
        do_something_else()
        # we were just testing and we don't really want to do this
        tx.rollback()

  # and here the connection is closed
Any chance of pushing some changes up into the postgresql side? I'm thinking of a block of trx with params separate? Didn't look at it at all.
3 comments

For what it's worth, using context managers for this kind of thing is pretty much standard nowadays, and not supporting this behavior feels jarring and archaic.
Agreed - I spent so much time trouble shooting this exact issue because I honestly couldn't even imagine that I wasn't freeing the connection.

I ended just giving up and doing a close() after searching for every use. But even then didn't immediately understand what was going on.

With XXX do Y

is totally standard - but here we open but do not close.

Could you expand on the "pushing into pg" bit?

Do you want to specify parameters that have a scope larger than a single statement and send the whole parameterized batch in one go?

Ruh roh I have some code to fix. :sweat_smile: