[Checkins]
SVN: z3c.sqlalchemy/branches/zope.sqlalchemy-integration/src/z3c/sqlalchemy/base.py
first some cleanup
Andreas Jung
andreas at andreas-jung.com
Thu May 8 14:09:36 EDT 2008
Log message for revision 86542:
first some cleanup
Changed:
U z3c.sqlalchemy/branches/zope.sqlalchemy-integration/src/z3c/sqlalchemy/base.py
-=-
Modified: z3c.sqlalchemy/branches/zope.sqlalchemy-integration/src/z3c/sqlalchemy/base.py
===================================================================
--- z3c.sqlalchemy/branches/zope.sqlalchemy-integration/src/z3c/sqlalchemy/base.py 2008-05-08 17:58:39 UTC (rev 86541)
+++ z3c.sqlalchemy/branches/zope.sqlalchemy-integration/src/z3c/sqlalchemy/base.py 2008-05-08 18:09:35 UTC (rev 86542)
@@ -24,32 +24,13 @@
import transaction
from transaction.interfaces import ISavepointDataManager, IDataManagerSavepoint
+from sqlalchemy import *
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import scoped_session, sessionmaker, relation
+from zope.sqlalchemy import ZopeTransactionExtension, invalidate
+import transaction
-class SynchronizedThreadCache(object):
- def __init__(self):
- self.lock = threading.Lock()
- self.cache = threading.local()
-
- def set(self, id, d):
- self.lock.acquire()
- setattr(self.cache, id, d)
- self.lock.release()
-
- def get(self, id):
- self.lock.acquire()
- result = getattr(self.cache, id, None)
- self.lock.release()
- return result
-
- def remove(self, id):
- self.lock.acquire()
- if hasattr(self.cache, id):
- delattr(self.cache, id)
- self.lock.release()
-
-
-
class BaseWrapper(object):
implements(ISQLAlchemyWrapper)
@@ -151,106 +132,7 @@
**self.session_options)
-connection_cache = SynchronizedThreadCache()
-
-class SessionDataManager(object):
- """ Wraps session into transaction context of Zope """
-
- implements(ISavepointDataManager)
-
- def __init__(self, connection, session, id, transactional=True):
-
- self.connection = connection
- self.session = session
- self.transactional = True
- self._id = id
- self.transaction = None
- if self.transactional:
- self.transaction = connection.begin()
-
- def abort(self, trans):
-
- try:
- if self.transaction is not None:
- self.transaction.rollback()
- # DM: done in "_cleanup" (similar untidy code at other places as well)
-## self.session.clear()
-## connection_cache.remove(self._id)
- finally:
- # ensure '_cleanup' is called even when 'rollback' causes an exception
- self._cleanup()
-
- def _flush(self):
-
- # check if the session contains something flushable
- if self.session.new or self.session.deleted or self.session.dirty:
-
- # Check if a session-bound transaction has been created so far.
- # If not, create a new transaction
-# if self.transaction is None:
-# self.transaction = connection.begin()
-
- # Flush
- self.session.flush()
-
- def commit(self, trans):
- self._flush()
-
- def tpc_begin(self, trans):
- pass
-
- def tpc_vote(self, trans):
- self._flush()
-
- def tpc_finish(self, trans):
-
- if self.transaction is not None:
- self.transaction.commit()
-
- self.session.clear()
- self._cleanup()
-
-
- # DM: no need to duplicate this code (identical to "abort")
-## def tpc_abort(self, trans):
-## if self.transaction is not None:
-## self.transaction.rollback()
-## self._cleanup()
- tpc_abort = abort
-
- def sortKey(self):
- return 'z3c.sqlalchemy_' + str(id(self))
-
- def _cleanup(self):
- self.session.clear()
- if self.connection:
- self.connection.close()
- self.connection = None
- connection_cache.remove(self._id)
- # DM: maybe, we should set "transaction" to "None"?
-
- def savepoint(self):
- """ return a dummy savepoint """
- return AlchemySavepoint()
-
-
-
-# taken from z3c.zalchemy
-
-class AlchemySavepoint(object):
- """A dummy saveoint """
-
- implements(IDataManagerSavepoint)
-
- def __init__(self):
- pass
-
- def rollback(self):
- pass
-
-
-
class ZopeBaseWrapper(BaseWrapper):
""" A wrapper to be used from within Zope. It connects
the session with the transaction management of Zope.
@@ -258,27 +140,8 @@
def __getOrCreateConnectionCacheItem(self, cache_id):
+ pass
- cache_item = connection_cache.get(cache_id)
-
- # return cached session if we are within the same transaction
- # and same thread
- if cache_item is not None:
- return cache_item
-
- # no cached session, let's create a new one
- connection = self.engine.connect()
- session = sessionmaker(connection)()
-
- # register a DataManager with the current transaction
- transaction.get().join(SessionDataManager(connection, session, self._id))
-
- # update thread-local cache
- cache_item = dict(connection=connection, session=session)
- connection_cache.set(self._id, cache_item)
- return cache_item
-
-
@property
def session(self):
""" Return a (cached) session object for the current transaction """
More information about the Checkins
mailing list