src/ztfy/utils/zodb.py
branchZTK-1.1
changeset 179 612a350cc660
parent 176 a7275e2a9252
child 181 3bdaee25ec3a
--- a/src/ztfy/utils/zodb.py	Tue Oct 16 08:17:49 2012 +0200
+++ b/src/ztfy/utils/zodb.py	Thu Oct 18 23:43:15 2012 +0200
@@ -45,6 +45,10 @@
 
     implements(IZEOConnection)
 
+    _storage = None
+    _db = None
+    _connection = None
+
     server_name = FieldProperty(IZEOConnection['server_name'])
     server_port = FieldProperty(IZEOConnection['server_port'])
     storage = FieldProperty(IZEOConnection['storage'])
@@ -79,6 +83,21 @@
         db = DB(storage)
         return (storage, db) if get_storage else db
 
+    @property
+    def connection(self):
+        return self._connection
+
+    def __enter__(self):
+        self._storage, self._db = self.getConnection(get_storage=True)
+        self._connection = self._db.open()
+        return self
+
+    def __exit__(self, exc_type, exc_value, traceback):
+        if self._connection is not None:
+            self._connection.close()
+        if self._storage is not None:
+            self._storage.close()
+
 
 class ZEOConnectionUtility(ZEOConnectionInfo, Persistent, Contained):
     """Persistent ZEO connection settings utility"""