I would like to offer the following small enhancements to the pgdbCnx class:
1. close() should ROLLBACK if there is a transaction underway.
To preserve current behaviour, it should ignore any OperationalError
that results from attempting the ROLLBACK.
Reason: we've found that if you close a connection to pgbouncer with a
transaction open, it can't return the postgresql connection at its end
back to the connection pool immediately. This badly degrades performance
if you have a lot of short-duration connections from (for example) web
services
funnelled through pgbouncer.
2. connect() should be usable in a python "with" statement so it
auto-closes at the end of the block. This just needs __enter__ and
__exit__ methods, which will have no effect on code not using the
connection in a "with".
A patch is attached, svn diff from revision 427
Peter Harris
Software Engineer
HuzuTech Ltd
Index: pgdb.py
===================================================================
--- pgdb.py (revision 427)
+++ pgdb.py (working copy)
@@ -434,6 +434,11 @@
def close(self):
"""Close the connection object."""
if self._cnx:
+ if self._tnx:
+ try:
+ self.rollback()
+ except OperationalError:
+ pass
self._cnx.close()
self._cnx = None
else:
@@ -473,7 +478,15 @@
else:
raise OperationalError("connection has been closed")
+ def __enter__(self):
+ """ return self if used in a with block """
+ return self
+ def __exit__(self, dummye, dummyv, dummyt):
+ """ close on exit if used in a with block """
+ self.close()
+ return False
+
### Module Interface
_connect_ = connect
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql