Ticket #1721: turbogears-sqlalchemy-0.4-rollback.patch

File turbogears-sqlalchemy-0.4-rollback.patch, 1.5 kB (added by toshio, 8 months ago)

Implement exception checking and rollback in sa_rwt()

  • TurboGears-1.0.4.2/turbogears/database.py

    old new  
    405405    try: 
    406406        retval = func(*args, **kw) 
    407407 
    408     except (cherrypy.HTTPRedirect, cherrypy.InternalRedirect)
     408    except (cherrypy.HTTPRedirect, cherrypy.InternalRedirect), e
    409409        log.debug('this is only a redirect') 
    410410        # If a redirect happens; commit and proceed with redirect 
    411411        if sa_tr_active(req.sa_transaction): 
    412             req.sa_transaction.commit() 
    413         raise 
     412            try: 
     413                req.sa_transaction.commit() 
     414            except sqlalchemy.exceptions.InvalidRequestError: 
     415                req.sa_transaction.rollback() 
     416        raise e 
    414417 
    415418    except: 
    416419        log.debug('this is an exception, ROLLBACK now!') 
     
    421424 
    422425    # If the call was successful; commit and proceed 
    423426    if sa_tr_active(req.sa_transaction): 
    424         log.debug('The transaction was successful, COMMIT now!') 
    425         req.sa_transaction.commit() 
     427        try: 
     428            log.debug('Transaction successful, try commit') 
     429            req.sa_transaction.commit() 
     430            log.debug('Commit successful') 
     431        except sqlalchemy.exceptions.InvalidRequestError: 
     432            log.debug('Commit unsuccessful, ROLLBACK now!') 
     433            req.sa_transaction.rollback() 
    426434 
    427435    return retval 
    428436