FSX / momoko

Wraps (asynchronous) Psycopg2 for Tornado.
http://momoko.61924.nl/
Other
359 stars 73 forks source link

Any suggestion on how I'd commit/rollback using a connection pool? #153

Closed vladiibine closed 7 years ago

vladiibine commented 7 years ago

Hi,

If I want to commit/rollback a transaction, is there any clean way of doing that, when using a connection pool?

[UPDATE] after a few hours:

I found that this way works

conn = yield pool.getconn()
with pool.manage(conn):
    try:
        cursor = yield conn.execute('select 1')
    except MyException:
        yield conn.execute('ROLLBACK')
    .....

While this works, and it commits the work, I still am wondering:

Thanks a lot in advance. After figuring these things out, I'll try make some time to help you update the documentation with these issues.

haizaar commented 7 years ago

Could I still avoid executting a commit after exiting the managed context?

Nothing in momoko lib calls COMMIT for you. I'm not sure why you believe it happens (may be Postgres does it behind the scenes in some way - I'm not much familiar with this DB). BTW, there is Connection.transaction method, that executes a list of statements and commits/rolls back. Does it help you?

Rolling back - I see indeed no rollback method on the connection

Again, there are no helper methods like Connection.commit and Connection.rollback - you can call execute with appropriate statements or extend connection class to add this methods (currently it will requires monkey-patching the imported package, but I can extend Pool to accept ConnectionClass in constructor).

After retrieving a cursor from yield pool.execute('select something') I'll get back a cursor...

Are named cursors what you are after?

haizaar commented 7 years ago

Closing this discussion due to inactivity. Feel free to reopen.

vladiibine commented 7 years ago

Yeah, I'm still thinking about it. If I have anything interesting I'll let you know. Thanks for the support! :)