[Fixed]-Psycopg2 pool SimpleConnectionPool needs commit?

1👍

Here’s a link to the psycopg2 documentation on transactions:

http://initd.org/psycopg/docs/usage.html#transactions-control

Quoting from the docs:

When a connection exits the with block, if no exception has been raised by the block, the transaction is committed. In case of exception the transaction is rolled back.

When a cursor exits the with block it is closed, releasing any resource eventually associated with it. The state of the transaction is not affected.

Note that, unlike file objects or other resources, exiting the connection’s with block doesn’t close the connection but only the transaction associated with it: a connection can be used in more than a with statement and each with block is effectively wrapped in a separate transaction

As written, your code will perform the select, but the cursor will close unless you use it inside the with block. The with construct is only available for version 2.5 or later.

Leave a comment