Add context management methode to dbapi.Connection
the connection is automaticly closed when exiting the context.
--- a/dbapi.py Wed Mar 31 11:41:20 2010 +0200
+++ b/dbapi.py Wed Mar 31 11:52:15 2010 +0200
@@ -367,6 +367,16 @@
return '<Connection %s (anonymous)>' % self.sessionid
return '<Connection %s>' % self.sessionid
+ def __enter__(self):
+ return self.cursor()
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ if exc_type is None:
+ self.commit()
+ else:
+ self.rollback()
+ return False #propagate the exception
+
def request(self):
return DBAPIRequest(self.vreg, self)