[session] explicitly take Connection object in close_cnx
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Wed, 26 Jun 2013 14:01:07 +0200
changeset 9105 55738c9dc26f
parent 9104 9dde49a393a4
child 9106 c14e8a2b4655
[session] explicitly take Connection object in close_cnx Now that ClientConnection explicitly reference and use the Connection object we do not need to use connectionid here. We can safely change this signature, ClientConnection is the only use of close_cnx for now.
repoapi.py
server/repository.py
server/session.py
--- a/repoapi.py	Wed Jun 26 11:41:53 2013 +0200
+++ b/repoapi.py	Wed Jun 26 14:01:07 2013 +0200
@@ -166,10 +166,9 @@
 
     def __exit__(self, exc_type, exc_val, exc_tb):
         self._open = False
-        cnxid = self._cnxid
         self._cnxid = None
         self._cnx.ctx_count -= 1
-        self._session.close_cnx(cnxid)
+        self._session.close_cnx(self._cnx)
         self._cnx = None
         if self._autoclose_session:
             # we have to call repo.close to unsure the repo properly forget the
--- a/server/repository.py	Wed Jun 26 11:41:53 2013 +0200
+++ b/server/repository.py	Wed Jun 26 14:01:07 2013 +0200
@@ -497,18 +497,19 @@
 
     def _build_user(self, session, eid):
         """return a CWUser entity for user with the given eid"""
-        cls = self.vreg['etypes'].etype_class('CWUser')
-        st = cls.fetch_rqlst(session.user, ordermethod=None)
-        st.add_eid_restriction(st.get_variable('X'), 'x', 'Substitute')
-        rset = session.execute(st.as_string(), {'x': eid})
-        assert len(rset) == 1, rset
-        cwuser = rset.get_entity(0, 0)
-        # pylint: disable=W0104
-        # prefetch / cache cwuser's groups and properties. This is especially
-        # useful for internal sessions to avoid security insertions
-        cwuser.groups
-        cwuser.properties
-        return cwuser
+        with session.ensure_cnx_set:
+            cls = self.vreg['etypes'].etype_class('CWUser')
+            st = cls.fetch_rqlst(session.user, ordermethod=None)
+            st.add_eid_restriction(st.get_variable('X'), 'x', 'Substitute')
+            rset = session.execute(st.as_string(), {'x': eid})
+            assert len(rset) == 1, rset
+            cwuser = rset.get_entity(0, 0)
+            # pylint: disable=W0104
+            # prefetch / cache cwuser's groups and properties. This is especially
+            # useful for internal sessions to avoid security insertions
+            cwuser.groups
+            cwuser.properties
+            return cwuser
 
     # public (dbapi) interface ################################################
 
--- a/server/session.py	Wed Jun 26 11:41:53 2013 +0200
+++ b/server/session.py	Wed Jun 26 14:01:07 2013 +0200
@@ -1305,13 +1305,11 @@
                 self._cnxs[cnxid] = cnx
         return cnx
 
-    def close_cnx(self, cnxid):
+    def close_cnx(self, cnx):
         """Close a Connection related to a session"""
-        cnx = self._cnxs.get(cnxid, None)
-        if cnx is not None:
-            cnx.free_cnxset(ignoremode=True)
-            self._clear_thread_storage(cnx)
-            cnx.clear()
+        cnx.free_cnxset(ignoremode=True)
+        self._clear_thread_storage(cnx)
+        cnx.clear()
 
     def set_cnx(self, cnxid=None):
         """set the default connection of the current thread to <cnxid>