Ticket #1721: session.close.patch

File session.close.patch, 2.4 kB (added by paj, 8 months ago)
  • turbogears/database.py

    old new  
     1 
    12"""Provides convenient access to an SQLObject or SQLAlchemy 
    23managed database.""" 
    34 
     
    403404    req.sa_transaction = make_sa_transaction(session) 
    404405 
    405406    try: 
    406         retval = func(*args, **kw) 
     407        try: 
     408            retval = func(*args, **kw) 
    407409 
    408     except (cherrypy.HTTPRedirect, cherrypy.InternalRedirect): 
    409         log.debug('this is only a redirect') 
    410         # If a redirect happens; commit and proceed with redirect 
     410        except (cherrypy.HTTPRedirect, cherrypy.InternalRedirect): 
     411            log.debug('this is only a redirect') 
     412            # If a redirect happens; commit and proceed with redirect 
     413            if sa_tr_active(req.sa_transaction): 
     414                req.sa_transaction.commit() 
     415            raise 
     416 
     417        except: 
     418            log.debug('this is an exception, ROLLBACK now!') 
     419            # If any other exception happens; rollback and re-raise error 
     420            if sa_tr_active(req.sa_transaction): 
     421                req.sa_transaction.rollback() 
     422            raise 
     423 
     424        # If the call was successful; commit and proceed 
    411425        if sa_tr_active(req.sa_transaction): 
     426            log.debug('The transaction was successful, COMMIT now!') 
    412427            req.sa_transaction.commit() 
    413         raise 
     428    finally: 
     429        # With SA 0.4, always close the session 
     430        if not hasattr(session, 'context'): 
     431            session.close() 
    414432 
    415     except: 
    416         log.debug('this is an exception, ROLLBACK now!') 
    417         # If any other exception happens; rollback and re-raise error 
    418         if sa_tr_active(req.sa_transaction): 
    419             req.sa_transaction.rollback() 
    420         raise 
    421  
    422     # If the call was successful; commit and proceed 
    423     if sa_tr_active(req.sa_transaction): 
    424         log.debug('The transaction was successful, COMMIT now!') 
    425         req.sa_transaction.commit() 
    426  
    427433    return retval 
    428434 
    429435# include "args" to avoid call being pre-cached 
     
    441447        # SA 0.3 
    442448        return tr.session.transaction 
    443449    else: 
    444         # SA 0.4 (effectively always active - commit or rollback don't fail) 
     450        # SA 0.4 can be treated as always active; if no transaction 
     451        # is active, commit and rollback quietly do nothing 
    445452        return True 
    446453 
    447454def make_sa_transaction(session):