[z3-checkins] r22131 - in
z3/sqlos/branch/jinty-connection/src/sqlos: . ftests
jinty at codespeak.net
jinty at codespeak.net
Sat Jan 14 07:22:34 CET 2006
Author: jinty
Date: Sat Jan 14 07:22:29 2006
New Revision: 22131
Modified:
z3/sqlos/branch/jinty-connection/src/sqlos/_transaction.py
z3/sqlos/branch/jinty-connection/src/sqlos/adapter.py
z3/sqlos/branch/jinty-connection/src/sqlos/ftests/test_transaction.py
Log:
Move the cache to _transaction.py and register a synchronizer.
Modified: z3/sqlos/branch/jinty-connection/src/sqlos/_transaction.py
==============================================================================
--- z3/sqlos/branch/jinty-connection/src/sqlos/_transaction.py (original)
+++ z3/sqlos/branch/jinty-connection/src/sqlos/_transaction.py Sat Jan 14 07:22:29 2006
@@ -14,15 +14,56 @@
import sets
+import transaction
from transaction import get
-from transaction.interfaces import IDataManager
+from transaction.interfaces import IDataManager, ISynchronizer
from zope.interface import implements
from zope.app.event.objectevent import modified
from sqlobject import SQLObjectNotFound
+from sqlobject.cache import CacheSet
from zope.security.proxy import removeSecurityProxy
+from zope.thread import local
from sqlos.interfaces import ISQLObject
+cache = local() # global cache of sqlobjects, one per thread
+
+def getGlobalPerThreadCache():
+ c = getattr(cache, 'cacheset', None)
+ if c is None:
+ cache.cacheset = CacheSet()
+ c = cache.cacheset
+ return c
+
+class CacheSynchronizer:
+ """Synchronizer to expire the Global per thread cache at transaction start.
+
+ Let's check that it implements the interface correctly:
+
+ >>> from zope.interface import verify
+ >>> synch = CacheSynchronizer()
+ >>> verify.verifyObject(ISynchronizer, synch)
+ True
+ """
+ # XXX this is probably the wrong place to put this code.
+ implements(ISynchronizer)
+
+ def afterCompletion(self, transaction):
+ pass
+
+ def beforeCompletion(self, transaction):
+ pass
+
+ def newTransaction(self, transaction):
+ cacheset = getGlobalPerThreadCache()
+ for cache in cacheset.allSubCaches():
+ cache.clear() # Blech, not optimal, don't know what is.
+ # expireAll, doesn't expire the objects.
+
+_synch = CacheSynchronizer() # we need to keep a solid reference, because the
+ # transaction manager only keeps weak ones
+transaction.manager.registerSynch(_synch)
+
def beforeCommitHook(obj):
"""Called before transactions are started.
Modified: z3/sqlos/branch/jinty-connection/src/sqlos/adapter.py
==============================================================================
--- z3/sqlos/branch/jinty-connection/src/sqlos/adapter.py (original)
+++ z3/sqlos/branch/jinty-connection/src/sqlos/adapter.py Sat Jan 14 07:22:29 2006
@@ -16,25 +16,13 @@
from sqlobject import _mysql, _postgres, _sybase, _sqlite
from sqlobject.converters import registerConverter
from sqlobject.mysql import mysqlconnection
-from sqlobject.cache import CacheSet
from zope.app.rdb.interfaces import DatabaseException
from zope.app.container.interfaces import INameChooser
-from zope.thread import local
+from zope.interface import implements
from sqlos.interfaces import ISQLObject
from sqlos._transaction import SQLObjectTransactionManager
-
-# XXX - if this is made into a thread local thing, then the expireSQLObject hook needs
-# to expire the object in all caches of other threads.
-
-cache = local() # global cache of sqlobjects, one per thread
-
-def getGlobalPerThreadCache():
- c = getattr(cache, 'cacheset', None)
- if c is None:
- cache.cacheset = CacheSet()
- c = cache.cacheset
- return c
+from sqlos._transaction import getGlobalPerThreadCache
class ConnectionAdapter:
@@ -112,7 +100,7 @@
class SybaseAdapter(ConnectionAdapter, _sybase.builder()):
pass
- # XXX - this is probably broken, feel free to fix it - jinty
+ # XXX - this is surely broken, feel free to fix it - jinty
class PostgresAdapter(ConnectionAdapter, _postgres.builder()):
Modified: z3/sqlos/branch/jinty-connection/src/sqlos/ftests/test_transaction.py
==============================================================================
--- z3/sqlos/branch/jinty-connection/src/sqlos/ftests/test_transaction.py (original)
+++ z3/sqlos/branch/jinty-connection/src/sqlos/ftests/test_transaction.py Sat Jan 14 07:22:29 2006
@@ -146,7 +146,7 @@
thread = threading.Thread(target=changePerson)
thread.start()
thread.join()
- # start a new transaction in this one
+ # start a new transaction in this thread
get().commit()
begin()
# If we get a new person, we should see the changed person
More information about the z3-checkins
mailing list