[z3-checkins] r22110 - z3/sqlos/branch/jinty-connection/src/sqlos

jinty at codespeak.net jinty at codespeak.net
Fri Jan 13 14:34:24 CET 2006


Author: jinty
Date: Fri Jan 13 14:34:19 2006
New Revision: 22110

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/connection.py
Log:
Remove our one connection per thread cache. We don't need it anymore as
zope.app.rdb has one as well since 30682. And that's the right place for
this kind of stuff.

Also this solves a serious bug where if the connection was disconnected
and zope.app.rdb dealt with this (though it doesn't yet), then the
connection would stay in the cache forever requiring a server restart.


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	Fri Jan 13 14:34:19 2006
@@ -22,7 +22,6 @@
 from zope.security.proxy import removeSecurityProxy
 
 from sqlos.interfaces import ISQLObject
-from sqlos.connection import connCache
 
 def beforeCommitHook(obj):
     """Called before transactions are started.
@@ -46,8 +45,6 @@
     This function should not cause SQL to be sent as it is not defined whether
     the SQL connection will commit before or after this is executed.
     """
-    for connection in connCache.values():
-        connection.cache.expire(obj.id, obj.__class__)
 
     # Expire object values. The transaction has either been aborted or
     # committed.

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	Fri Jan 13 14:34:19 2006
@@ -16,6 +16,7 @@
 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
@@ -23,6 +24,15 @@
 from sqlos.interfaces import ISQLObject
 from sqlos._transaction import SQLObjectTransactionManager
 
+# I am not sure it is a good idea to have a single global cache. or if it is
+# thread safe.  SQLObject seems to think so. We need some more tests to
+# determine this. As a positive, there is no need to look for all caches to
+# expire the objects when committing a transaction.
+
+# 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 = CacheSet() # global cache of sqlobjects
 
 class ConnectionAdapter:
 
@@ -30,6 +40,7 @@
         DBAPI.__init__(self)
         self._connection = connection
         self.autoCommit = None
+        self.cache = cache # use a global cache
         self.debug = 0
         self.supportTransactions = False
         self._dm = SQLObjectTransactionManager()

Modified: z3/sqlos/branch/jinty-connection/src/sqlos/connection.py
==============================================================================
--- z3/sqlos/branch/jinty-connection/src/sqlos/connection.py	(original)
+++ z3/sqlos/branch/jinty-connection/src/sqlos/connection.py	Fri Jan 13 14:34:19 2006
@@ -43,64 +43,19 @@
                               SQLObjectWarning, 2)
                 return
             name = ut.name
+        newconn = zapi.queryUtility(IZopeDatabaseAdapter, name,
+                                    default=None,
+                                    context=context)
+        if newconn is None:
+            warnings.warn("Couldn't find a rdb connection by the "
+                          "name %s. Please verify your setup." % name,
+                          SQLObjectWarning, 3)
         try:
-            return getConnection(context, name)
+            return IZopeSQLConnection(newconn())
         except ComponentLookupError:
             return self
 
     def __set__(self, inst, value):
         # Ignore, so we don't get overriden.
-        # We always use the connections from the
-        # global thread cache.
+        # We always use the connections from the IZopeDatabaseAdapter utility.
         pass
-
-# Connection cache, one per thread, a-la Transaction.
-# This code was heavily based on ZODB.Transaction
-connCache = {}
-
-try:
-    import thread
-except:
-    def getConnection(context, name):
-        global connCache
-        if not connCache.get(name):
-            newconn = zapi.queryUtility(IZopeDatabaseAdapter, name,
-                                        default=None,
-                                        context=context)
-            if newconn is None:
-                warnings.warn("Couldn't find a rdb connection by the "
-                              "name %s. Please verify your setup." % name,
-                              SQLObjectWarning, 3)
-            connCache[name] = IZopeSQLConnection(newconn())
-        return connCache[name]
-
-    def releaseConnection(name):
-        global connCache
-        if connCache.has_key(name):
-            connCache[name].close()
-            del connCache[name]
-else:
-    def getConnection(context, name):
-        global connCache
-        tid = thread.get_ident()
-        key = (tid, name)
-        if not connCache.get(key):
-            newconn = zapi.queryUtility(IZopeDatabaseAdapter, name,
-                                        default=None,
-                                        context=context)
-            if newconn is None:
-                warnings.warn("Couldn't find a rdb connection by the "
-                              "name %s. Please verify your setup." % name,
-                              SQLObjectWarning, 3)
-            conn = IZopeSQLConnection(newconn())
-            connCache[key] = conn
-
-        return connCache[key]
-
-    def releaseConnection(name):
-        global connCache
-        tid = thread.get_ident()
-        key = (tid, name)
-        if connCache.has_key(key):
-            connCache[key].close()
-            del connCache[key]


More information about the z3-checkins mailing list