[Checkins] SVN: z3c.zalchemy/branches/ctheune-automatic-session/src/z3c/zalchemy/datamanager.py Refactored explicit binding of classes and tables so that new sessions are

Christian Theune ct at gocept.com
Thu Jun 28 01:29:18 EDT 2007


Log message for revision 77162:
  Refactored explicit binding of classes and tables so that new sessions are
  correctly bound.
  

Changed:
  U   z3c.zalchemy/branches/ctheune-automatic-session/src/z3c/zalchemy/datamanager.py

-=-
Modified: z3c.zalchemy/branches/ctheune-automatic-session/src/z3c/zalchemy/datamanager.py
===================================================================
--- z3c.zalchemy/branches/ctheune-automatic-session/src/z3c/zalchemy/datamanager.py	2007-06-28 04:35:50 UTC (rev 77161)
+++ z3c.zalchemy/branches/ctheune-automatic-session/src/z3c/zalchemy/datamanager.py	2007-06-28 05:29:18 UTC (rev 77162)
@@ -82,14 +82,32 @@
 # manager.
 
 def createSession():
+    """Creates a new session that is bound to the default engine utility and
+    hooked up with the Zope transaction machinery.
+
+    """
     util = queryUtility(IAlchemyEngineUtility)
     if util is None:
         raise ValueError("No engine utility registered")
     engine = util.getEngine()
     session = sqlalchemy.create_session(bind_to=engine)
+
+    # This session is now only bound to the default engine. We need to bind
+    # the other explicitly bound tables and classes as well.
+    bind_session(session)
+
     transaction.get().join(AlchemyDataManager(session))
     return session
 
+
+def bind_session(session):
+    """Applies all table and class bindings to the given session."""
+    for table, engine in _tableToEngine.items():
+        _assignTable(table, engine, session)
+    for class_, engine in _classToEngine.items():
+        _assignClass(class_, engine, session)
+
+
 ctx = SessionContext(createSession)
 global_extensions.append(ctx.mapper_extension)
 
@@ -123,16 +141,20 @@
     _createTables()
 
 
-def _assignTable(table, engine):
+def _assignTable(table, engine, session=None):
     t = metadata.getTable(engine, table, True)
     util = getUtility(IAlchemyEngineUtility, name=engine)
-    ctx.current.bind_table(t, util.getEngine())
+    if session is None:
+        session = ctx.current
+    session.bind_table(t, util.getEngine())
 
 
-def _assignClass(class_, engine):
+def _assignClass(class_, engine, session=None):
     m = sqlalchemy.orm.class_mapper(class_)
     util = getUtility(IAlchemyEngineUtility, name=engine)
-    ctx.current.bind_mapper(m,util.getEngine())
+    if session is None:
+        session = ctx.current
+    session.bind_mapper(m,util.getEngine())
 
 
 def _createTables():



More information about the Checkins mailing list