server/session.py
branchstable
changeset 7451 48ba5f0c11de
parent 7405 8c752d113ebb
child 7454 1090724f28ed
child 7500 cb0f4da64e86
equal deleted inserted replaced
7450:c21d845836e4 7451:48ba5f0c11de
    73     user session but an internal session
    73     user session but an internal session
    74     """
    74     """
    75     return req.is_internal_session
    75     return req.is_internal_session
    76 
    76 
    77 
    77 
       
    78 class transaction(object):
       
    79     """context manager to enter a transaction for a session: when exiting the
       
    80     `with` block on exception, call `session.rollback()`, else call
       
    81     `session.commit()` on normal exit
       
    82     """
       
    83     def __init__(self, session, free_cnxset=True):
       
    84         self.session = session
       
    85         self.free_cnxset = free_cnxset
       
    86 
       
    87     def __enter__(self):
       
    88         pass
       
    89 
       
    90     def __exit__(self, exctype, exc, traceback):
       
    91         if exctype:
       
    92             self.session.rollback(free_cnxset=self.free_cnxset)
       
    93         else:
       
    94             self.session.commit(free_cnxset=self.free_cnxset)
       
    95 
       
    96 
    78 class hooks_control(object):
    97 class hooks_control(object):
    79     """context manager to control activated hooks categories.
    98     """context manager to control activated hooks categories.
    80 
    99 
    81     If mode is session.`HOOKS_DENY_ALL`, given hooks categories will
   100     If mode is session.`HOOKS_DENY_ALL`, given hooks categories will
    82     be enabled.
   101     be enabled.
   185         self._closed_lock = threading.Lock()
   204         self._closed_lock = threading.Lock()
   186 
   205 
   187     def __unicode__(self):
   206     def __unicode__(self):
   188         return '<%ssession %s (%s 0x%x)>' % (
   207         return '<%ssession %s (%s 0x%x)>' % (
   189             self.cnxtype, unicode(self.user.login), self.id, id(self))
   208             self.cnxtype, unicode(self.user.login), self.id, id(self))
       
   209 
       
   210     def transaction(self, free_cnxset=True):
       
   211         """return context manager to enter a transaction for the session: when
       
   212         exiting the `with` block on exception, call `session.rollback()`, else
       
   213         call `session.commit()` on normal exit.
       
   214 
       
   215         The `free_cnxset` will be given to rollback/commit methods to indicate
       
   216         wether the connections set should be freed or not.
       
   217         """
       
   218         return transaction(self, free_cnxset)
   190 
   219 
   191     def set_tx_data(self, txid=None):
   220     def set_tx_data(self, txid=None):
   192         if txid is None:
   221         if txid is None:
   193             txid = threading.currentThread().getName()
   222             txid = threading.currentThread().getName()
   194         try:
   223         try: