server/session.py
changeset 9107 1e145b36edf0
parent 9106 c14e8a2b4655
child 9108 6a4070e2849d
equal deleted inserted replaced
9106:c14e8a2b4655 9107:1e145b36edf0
   429 
   429 
   430     """
   430     """
   431 
   431 
   432     is_request = False
   432     is_request = False
   433 
   433 
   434     def __init__(self, session, cnxid):
   434     def __init__(self, session, cnxid, session_handled=False):
   435         # using super(Connection, self) confuse some test hack
   435         # using super(Connection, self) confuse some test hack
   436         RequestSessionBase.__init__(self, session.vreg)
   436         RequestSessionBase.__init__(self, session.vreg)
   437         #: connection unique id
   437         #: connection unique id
   438         self.connectionid = cnxid
   438         self.connectionid = cnxid
       
   439         #: self._session_handled
       
   440         #: are the life cycle of this Connection automatically controlled by the
       
   441         #: Session This is the old backward compatibility mode
       
   442         self._session_handled = session_handled
   439         #: reentrance handling
   443         #: reentrance handling
   440         self.ctx_count = 0
   444         self.ctx_count = 0
   441         #: count the number of entry in a context needing a cnxset
   445         #: count the number of entry in a context needing a cnxset
   442         self._cnxset_count = 0
   446         self._cnxset_count = 0
   443         #: Boolean for compat with the older explicite set_cnxset/free_cnx API
   447         #: Boolean for compat with the older explicite set_cnxset/free_cnx API
  1298             try:
  1302             try:
  1299                 if self.closed:
  1303                 if self.closed:
  1300                     raise SessionClosedError('try to access connections set on'
  1304                     raise SessionClosedError('try to access connections set on'
  1301                                              ' a closed session %s' % self.id)
  1305                                              ' a closed session %s' % self.id)
  1302                 cnx = self._cnxs[cnxid]
  1306                 cnx = self._cnxs[cnxid]
       
  1307                 assert cnx._session_handled
  1303             except KeyError:
  1308             except KeyError:
  1304                 cnx = Connection(self, cnxid=cnxid)
  1309                 cnx = Connection(self, cnxid=cnxid, session_handled=True)
  1305                 self._cnxs[cnxid] = cnx
  1310                 self._cnxs[cnxid] = cnx
  1306         return cnx
  1311         return cnx
  1307 
  1312 
  1308     def close_cnx(self, cnx):
  1313     def close_cnx(self, cnx):
  1309         """Close a Connection related to a session"""
  1314         """Close a Connection related to a session"""
       
  1315         assert cnx._session_handled
  1310         cnx.free_cnxset(ignoremode=True)
  1316         cnx.free_cnxset(ignoremode=True)
  1311         self._cnxs.pop(cnx.connectionid, None)
  1317         self._cnxs.pop(cnx.connectionid, None)
  1312         try:
  1318         try:
  1313             if self.__threaddata.cnx is cnx:
  1319             if self.__threaddata.cnx is cnx:
  1314                 del self.__threaddata.cnx
  1320                 del self.__threaddata.cnx
  1320         """set the default connection of the current thread to <cnxid>
  1326         """set the default connection of the current thread to <cnxid>
  1321 
  1327 
  1322         Connection is created if necessary"""
  1328         Connection is created if necessary"""
  1323         if cnxid is None:
  1329         if cnxid is None:
  1324             cnxid = threading.currentThread().getName()
  1330             cnxid = threading.currentThread().getName()
  1325         self.__threaddata.cnx = self.get_cnx(cnxid)
  1331         cnx = self.get_cnx(cnxid)
       
  1332         # New style session should not be accesed through the session.
       
  1333         assert cnx._session_handled
       
  1334         self.__threaddata.cnx = cnx
  1326 
  1335 
  1327     @property
  1336     @property
  1328     def _cnx(self):
  1337     def _cnx(self):
  1329         """default connection for current session in current thread"""
  1338         """default connection for current session in current thread"""
  1330         try:
  1339         try: