[Checkins] SVN: z3c.zalchemy/trunk/src/z3c/zalchemy/datamanager.py using the threadlocal strategy of sqlalchemy instead of creating one engine per thread. This allows connection pooling for instance.

Christian Zagrodnick cz at gocept.com
Wed Aug 8 04:53:01 EDT 2007


Log message for revision 78682:
  using the threadlocal strategy of sqlalchemy instead of creating one engine per thread. This allows connection pooling for instance.

Changed:
  U   z3c.zalchemy/trunk/src/z3c/zalchemy/datamanager.py

-=-
Modified: z3c.zalchemy/trunk/src/z3c/zalchemy/datamanager.py
===================================================================
--- z3c.zalchemy/trunk/src/z3c/zalchemy/datamanager.py	2007-08-08 03:59:49 UTC (rev 78681)
+++ z3c.zalchemy/trunk/src/z3c/zalchemy/datamanager.py	2007-08-08 08:53:01 UTC (rev 78682)
@@ -11,8 +11,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-import thread
-from threading import local
 
 import persistent
 import transaction
@@ -35,7 +33,8 @@
     """
     implements(IAlchemyEngineUtility)
 
-    def __init__(self, name, dsn, echo=False, encoding='utf-8', convert_unicode=False, **kwargs):
+    def __init__(self, name, dsn, echo=False, encoding='utf-8',
+                 convert_unicode=False, **kwargs):
         self.name = name
         self.dsn = dsn
         self.encoding = encoding
@@ -45,34 +44,29 @@
         self.kw.update(kwargs)
 
     def getEngine(self):
-        engine = getattr(self.storage,'engine',None)
+        engine = getattr(self,'engine',None)
         if engine:
             return engine
         # create_engine consumes the keywords, so better to make a copy first
         kw = {}
         kw.update(self.kw)
-        # create a new engine and store it thread local
-        self.storage.engine = sqlalchemy.create_engine(self.dsn,
-                                            echo=self.echo,
-                                            encoding=self.encoding,
-                                            convert_unicode=self.convert_unicode,
-                                            **kw)
-        return self.storage.engine
+        # create a new engine and configure it thread local
+        self.engine = sqlalchemy.create_engine(
+            self.dsn, echo=self.echo, encoding=self.encoding,
+            convert_unicode=self.convert_unicode,
+            strategy='threadlocal', **kw)
+        return self.engine
 
     def _resetEngine(self):
-        engine = getattr(self.storage, 'engine', None)
+        engine = getattr(self, 'engine', None)
         if engine is not None:
             engine.dispose()
-            self.storage.engine = None
+            self.engine = None
 
-    @property
-    def storage(self):
-        if not hasattr(self, '_v_storage'):
-            self._v_storage = local()
-        return self._v_storage
 
 for name in IAlchemyEngineUtility:
-    setattr(AlchemyEngineUtility, name, FieldProperty(IAlchemyEngineUtility[name]))
+    setattr(AlchemyEngineUtility, name, FieldProperty(
+        IAlchemyEngineUtility[name]))
 
 
 _tableToEngine = {}



More information about the Checkins mailing list