server/session.py
changeset 7454 1090724f28ed
parent 7406 e772a2c57b00
parent 7451 48ba5f0c11de
child 7514 32081892850e
equal deleted inserted replaced
7447:d5705c9bbe82 7454:1090724f28ed
    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.
   234         self._closed_lock = threading.Lock()
   253         self._closed_lock = threading.Lock()
   235 
   254 
   236     def __unicode__(self):
   255     def __unicode__(self):
   237         return '<%ssession %s (%s 0x%x)>' % (
   256         return '<%ssession %s (%s 0x%x)>' % (
   238             self.cnxtype, unicode(self.user.login), self.id, id(self))
   257             self.cnxtype, unicode(self.user.login), self.id, id(self))
       
   258 
       
   259     def transaction(self, free_cnxset=True):
       
   260         """return context manager to enter a transaction for the session: when
       
   261         exiting the `with` block on exception, call `session.rollback()`, else
       
   262         call `session.commit()` on normal exit.
       
   263 
       
   264         The `free_cnxset` will be given to rollback/commit methods to indicate
       
   265         wether the connections set should be freed or not.
       
   266         """
       
   267         return transaction(self, free_cnxset)
   239 
   268 
   240     def set_tx_data(self, txid=None):
   269     def set_tx_data(self, txid=None):
   241         if txid is None:
   270         if txid is None:
   242             txid = threading.currentThread().getName()
   271             txid = threading.currentThread().getName()
   243         try:
   272         try: