server/session.py
changeset 8843 ea05b8545dd8
parent 8842 63fa6b02b241
child 8844 168cf5ef21f8
equal deleted inserted replaced
8842:63fa6b02b241 8843:ea05b8545dd8
   339       :attr:`cnxset`, the connections set to use to execute queries on sources.
   339       :attr:`cnxset`, the connections set to use to execute queries on sources.
   340       If the transaction is read only, the connection set may be freed between
   340       If the transaction is read only, the connection set may be freed between
   341       actual query. This allows multiple transaction with a reasonable low
   341       actual query. This allows multiple transaction with a reasonable low
   342       connection set pool size. control mechanism is detailed below
   342       connection set pool size. control mechanism is detailed below
   343 
   343 
       
   344     .. automethod:: cubicweb.server.session.Transaction.set_cnxset
       
   345     .. automethod:: cubicweb.server.session.Transaction.free_cnxset
       
   346 
   344       :attr:`mode`, string telling the connections set handling mode, may be one
   347       :attr:`mode`, string telling the connections set handling mode, may be one
   345       of 'read' (connections set may be freed), 'write' (some write was done in
   348       of 'read' (connections set may be freed), 'write' (some write was done in
   346       the connections set, it can't be freed before end of the transaction),
   349       the connections set, it can't be freed before end of the transaction),
   347       'transaction' (we want to keep the connections set during all the
   350       'transaction' (we want to keep the connections set during all the
   348       transaction, with or without writing)
   351       transaction, with or without writing)
   456                 self._cnxset_tracker.forget(self.transactionid, old_cnxset)
   459                 self._cnxset_tracker.forget(self.transactionid, old_cnxset)
   457             if new_cnxset is not None:
   460             if new_cnxset is not None:
   458                 self._cnxset_tracker.record(self.transactionid, new_cnxset)
   461                 self._cnxset_tracker.record(self.transactionid, new_cnxset)
   459                 self._cnxset = new_cnxset
   462                 self._cnxset = new_cnxset
   460                 self.ctx_count += 1
   463                 self.ctx_count += 1
       
   464 
       
   465     def set_cnxset(self):
       
   466         """the transaction need a connections set to execute some queries"""
       
   467         if self.cnxset is None:
       
   468             cnxset = self.repo._get_cnxset()
       
   469             try:
       
   470                 self.cnxset = cnxset
       
   471                 try:
       
   472                     cnxset.cnxset_set()
       
   473                 except:
       
   474                     self.cnxset = None
       
   475                     raise
       
   476             except:
       
   477                 self.repo._free_cnxset(cnxset)
       
   478                 raise
       
   479         return self.cnxset
       
   480 
       
   481     def free_cnxset(self, ignoremode=False):
       
   482         """the transaction is no longer using its connections set, at least for some time"""
       
   483         # cnxset may be none if no operation has been done since last commit
       
   484         # or rollback
       
   485         cnxset = self.cnxset
       
   486         if cnxset is not None and (ignoremode or self.mode == 'read'):
       
   487             try:
       
   488                 self.cnxset = None
       
   489             finally:
       
   490                 cnxset.cnxset_freed()
       
   491                 self.repo._free_cnxset(cnxset)
       
   492 
   461 
   493 
   462     # Entity cache management #################################################
   494     # Entity cache management #################################################
   463     #
   495     #
   464     # The transaction entity cache as held in tx.data it is removed at end the
   496     # The transaction entity cache as held in tx.data it is removed at end the
   465     # end of the transaction (commit and rollback)
   497     # end of the transaction (commit and rollback)
  1055         """the session need a connections set to execute some queries"""
  1087         """the session need a connections set to execute some queries"""
  1056         with self._lock: # can probably be removed
  1088         with self._lock: # can probably be removed
  1057             if self._closed:
  1089             if self._closed:
  1058                 self.free_cnxset(True)
  1090                 self.free_cnxset(True)
  1059                 raise Exception('try to set connections set on a closed session %s' % self.id)
  1091                 raise Exception('try to set connections set on a closed session %s' % self.id)
  1060             tx = self._tx
  1092             return self._tx.set_cnxset()
  1061             if tx.cnxset is None:
  1093     free_cnxset = tx_meth('free_cnxset')
  1062                 cnxset = self.repo._get_cnxset()
       
  1063                 try:
       
  1064                     self._tx.cnxset = cnxset
       
  1065                     try:
       
  1066                         cnxset.cnxset_set()
       
  1067                     except:
       
  1068                         self._tx.cnxset = None
       
  1069                         raise
       
  1070                 except:
       
  1071                     self.repo._free_cnxset(cnxset)
       
  1072                     raise
       
  1073         return tx.cnxset
       
  1074 
       
  1075     def free_cnxset(self, ignoremode=False):
       
  1076         """the session is no longer using its connections set, at least for some time"""
       
  1077         # cnxset may be none if no operation has been done since last commit
       
  1078         # or rollback
       
  1079         tx = self._tx
       
  1080         cnxset = tx.cnxset
       
  1081         if cnxset is not None and (ignoremode or self.mode == 'read'):
       
  1082             try:
       
  1083                 tx.cnxset = None
       
  1084             finally:
       
  1085                 cnxset.cnxset_freed()
       
  1086                 self.repo._free_cnxset(cnxset)
       
  1087 
  1094 
  1088     def _touch(self):
  1095     def _touch(self):
  1089         """update latest session usage timestamp and reset mode to read"""
  1096         """update latest session usage timestamp and reset mode to read"""
  1090         self.timestamp = time()
  1097         self.timestamp = time()
  1091         self.local_perm_cache.clear() # XXX simply move in tx.data, no?
  1098         self.local_perm_cache.clear() # XXX simply move in tx.data, no?