server/pool.py
changeset 9463 d62e13eba033
parent 9456 a79e88aad555
child 9464 6f4f710aef83
--- a/server/pool.py	Thu Jun 27 08:52:15 2013 +0200
+++ b/server/pool.py	Wed Jan 22 15:49:32 2014 +0100
@@ -17,73 +17,57 @@
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 """CubicWeb server connections set : the repository has a limited number of
 :class:`ConnectionsSet` (defined in configuration, default to 4). Each of them
-hold a connection for each source used by the repository.
+hold a connection to the system source.
 """
 
 __docformat__ = "restructuredtext en"
 
 import sys
 
+from logilab.common.deprecation import deprecated
+
+
 class ConnectionsSet(object):
-    """handle connections on a set of sources, at some point associated to a
+    """handle connection to the system source, at some point associated to a
     :class:`Session`
     """
 
     # since 3.19, we only have to manage the system source connection
     def __init__(self, system_source):
         # dictionary of (source, connection), indexed by sources'uri
-        self.source_cnxs = {}
-        self.source_cnxs['system'] = (system_source,
-                                      system_source.get_connection())
-        self._cursors = {}
-
-    def __getitem__(self, uri):
-        """subscription notation provide access to sources'cursors"""
-        assert uri == 'system'
-        try:
-            cursor = self._cursors[uri]
-        except KeyError:
-            cursor = self.source_cnxs[uri][1].cursor()
-            if cursor is not None:
-                # None possible on sources without cursor support such as ldap
-                self._cursors[uri] = cursor
-        return cursor
+        self._source = system_source
+        self.cnx = system_source.get_connection()
+        self.cu = self.cnx.cursor()
 
     def commit(self):
         """commit the current transaction for this user"""
-        # FIXME: what happends if a commit fail
-        # would need a two phases commit or like, but I don't know how to do
-        # this using the db-api...
-        for source, cnx in self.source_cnxs.itervalues():
-            # let exception propagates
-            cnx.commit()
+        # let exception propagates
+        self.cnx.commit()
 
     def rollback(self):
         """rollback the current transaction for this user"""
-        for source, cnx in self.source_cnxs.itervalues():
-            # catch exceptions, rollback other sources anyway
-            try:
-                cnx.rollback()
-            except Exception:
-                source.critical('rollback error', exc_info=sys.exc_info())
-                # error on rollback, the connection is much probably in a really
-                # bad state. Replace it by a new one.
-                self.reconnect(source)
+        # catch exceptions, rollback other sources anyway
+        try:
+            self.cnx.rollback()
+        except Exception:
+            self._source.critical('rollback error', exc_info=sys.exc_info())
+            # error on rollback, the connection is much probably in a really
+            # bad state. Replace it by a new one.
+            self.reconnect()
 
     def close(self, i_know_what_i_do=False):
         """close all connections in the set"""
         if i_know_what_i_do is not True: # unexpected closing safety belt
             raise RuntimeError('connections set shouldn\'t be closed')
-        for cu in self._cursors.itervalues():
-            try:
-                cu.close()
-            except Exception:
-                continue
-        for _, cnx in self.source_cnxs.itervalues():
-            try:
-                cnx.close()
-            except Exception:
-                continue
+        try:
+            self.cu.close()
+            self.cu = None
+        except Exception:
+            pass
+        try:
+            self.cnx.close()
+        except Exception:
+            pass
 
     # internals ###############################################################
 
@@ -93,49 +77,40 @@
 
     def cnxset_freed(self):
         """connections set is being freed from a session"""
-        for source, cnx in self.source_cnxs.itervalues():
-            source.cnxset_freed(cnx)
-
-    def sources(self):
-        """return the source objects handled by this connections set"""
-        # implementation details of flying insert requires the system source
-        # first
-        yield self.source_cnxs['system'][0]
+        self._source.cnxset_freed(self.cnx)
 
-    def source(self, uid):
-        """return the source object with the given uri"""
-        return self.source_cnxs[uid][0]
-
-    def connection(self, uid):
-        """return the connection on the source object with the given uri"""
-        return self.source_cnxs[uid][1]
-
-    def reconnect(self, source=None):
+    def reconnect(self):
         """reopen a connection for this source or all sources if none specified
         """
-        if source is None:
-            sources = self.sources()
-        else:
-            sources = (source,)
-        for source in sources:
-            try:
-                # properly close existing connection if any
-                self.source_cnxs[source.uri][1].close()
-            except Exception:
-                pass
-            source.info('trying to reconnect')
-            self.source_cnxs[source.uri] = (source, source.get_connection())
-            self._cursors.pop(source.uri, None)
+        try:
+            # properly close existing connection if any
+            self.cnx.close()
+        except Exception:
+            pass
+        self._source.info('trying to reconnect')
+        self.cnx = self._source.get_connection()
+        self.cu = self.cnx.cursor()
 
     def check_connections(self):
-        for source, cnx in self.source_cnxs.itervalues():
-            newcnx = source.check_connection(cnx)
-            if newcnx is not None:
-                self.reset_connection(source, newcnx)
+        newcnx = self._source.check_connection(self.cnx)
+        if newcnx is not None:
+            self.cnx = newcnx
+            self.cu = self.cnx.cursor()
+
+    @deprecated('[3.19] use .cu instead')
+    def __getitem__(self, uri):
+        assert uri == 'system'
+        return self.cu
 
-    def reset_connection(self, source, cnx):
-        self.source_cnxs[source.uri] = (source, cnx)
-        self._cursors.pop(source.uri, None)
+    @deprecated('[3.19] use repo.system_source instead')
+    def source(self, uid):
+        assert uid == 'system'
+        return self._source
+
+    @deprecated('[3.19] use .cnx instead')
+    def connection(self, uid):
+        assert uid == 'system'
+        return self.cnx
 
 
 from cubicweb.server.hook import Operation, LateOperation, SingleLastOperation