[Checkins]
SVN: z3c.sqlalchemy/branches/reusing-zalchemy/src/z3c/sqlalchemy/base.py
cleanup
Andreas Jung
andreas at andreas-jung.com
Sat Mar 15 17:54:04 EDT 2008
Log message for revision 84681:
cleanup
Changed:
U z3c.sqlalchemy/branches/reusing-zalchemy/src/z3c/sqlalchemy/base.py
-=-
Modified: z3c.sqlalchemy/branches/reusing-zalchemy/src/z3c/sqlalchemy/base.py
===================================================================
--- z3c.sqlalchemy/branches/reusing-zalchemy/src/z3c/sqlalchemy/base.py 2008-03-15 21:24:34 UTC (rev 84680)
+++ z3c.sqlalchemy/branches/reusing-zalchemy/src/z3c/sqlalchemy/base.py 2008-03-15 21:54:03 UTC (rev 84681)
@@ -13,6 +13,8 @@
from sqlalchemy.engine.url import make_url
from sqlalchemy.orm import sessionmaker
+import transaction
+from transaction.interfaces import ISavepointDataManager, IDataManagerSavepoint
from zope.interface import implements
from zope.component import getUtility
from zope.component.interfaces import ComponentLookupError
@@ -20,36 +22,9 @@
from z3c.sqlalchemy.interfaces import ISQLAlchemyWrapper, IModelProvider
from z3c.sqlalchemy.model import Model
from z3c.sqlalchemy.mapper import LazyMapperCollection
+from z3c.zalchemy.tm import AlchemyEngineUtility, AlchemyDataManager
-import transaction
-from transaction.interfaces import ISavepointDataManager, IDataManagerSavepoint
-
-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)
@@ -84,8 +59,9 @@
self._createEngine()
self._id = str(random.random()) # used as unique key for session/connection cache
- from datamanager import *
- util = AlchemyEngineUtility(dsn=dsn, name=name)
+ util = AlchemyEngineUtility(dsn=dsn,
+ echo=kw.get('echo', ),
+ name='foo')
if model:
@@ -125,7 +101,9 @@
@property
def session(self):
- return self._sessionmaker()
+ session = self._sessionmaker()
+ transaction.get().join(AlchemyDataManager(session))
+ return session
def registerMapper(self, mapper, name):
self._mappers.registerMapper(mapper, name)
@@ -154,134 +132,12 @@
**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):
-
- if self.transaction is not None:
- self.transaction.rollback()
- self.session.clear()
- connection_cache.remove(self._id)
- 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()
-
-
- def tpc_abort(self, trans):
- if self.transaction is not None:
- self.transaction.rollback()
- self._cleanup()
-
- 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)
-
- 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.
"""
-
- def __getOrCreateConnectionCacheItem(self, cache_id):
-
- 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 """
- return self.__getOrCreateConnectionCacheItem(self._id)['session']
-
-
- @property
def connection(self):
""" This property is _private_ and only intented to be used
by SQLAlchemyDA and therefore it is not part of the
More information about the Checkins
mailing list