Changeset 3433

Show
Ignore:
Timestamp:
08/31/07 15:23:04 (1 year ago)
Author:
paj
Message:

Fix SA interaction with exception_handler

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/errorhandling.py

    r3320 r3433  
    1 import sys 
     1import sys 
    22from itertools import izip, islice 
    33from inspect import getargspec 
     
    6868    """Call function, catch and dispatch any resulting exception.""" 
    6969    # turbogears.database import here to avoid circular imports 
    70     from turbogears.database import _use_sa 
     70    from turbogears.database import _use_sa, session 
    7171    try: 
    7272        return func(self, *args, **kw) 
     
    8080            exc_type, exc_value, exc_trace = sys.exc_info() 
    8181            remove_keys(kw, ("tg_source", "tg_errors", "tg_exceptions")) 
     82            if _use_sa() and getattr(cherrypy.request, "in_transaction", None): 
     83                # using SA ? 
     84                # Caught pants down in the middle of a transaction :) ? 
     85                # rollback the transaction for this whole request 
     86                session.clear() 
     87                cherrypy.request.sa_transaction.rollback() 
    8288            try: 
    8389                output = dispatch_error(self, func, None, e, *args, **kw) 
     
    8692            else: 
    8793                del exc_trace 
    88  
    89                 if _use_sa() and getattr(cherrypy.request, "in_transaction", None): 
    90                     # using SA ? 
    91                     # Caught pants down in the middle of a transaction :) ? 
    92                     # rollback the transaction for this whole request 
    93                     cherrypy.request.sa_transaction.rollback() 
    94  
    9594                return output 
    9695 
  • branches/1.1/turbogears/tests/test_sqlalchemy.py

    r3431 r3433  
    146146    assert 'InvalidRequestError: This transaction is inactive' not in cherrypy.response.body[0] 
    147147 
     148# Check an exception within a tg.exception_handler causes a rollback 
     149class RbRoot(RootController): 
     150    def handerr(self, id): 
     151        Person(id=int(id)+1) 
     152        session.flush() 
     153        return dict() 
     154    @expose() 
     155    @errorhandling.exception_handler(handerr) 
     156    def doerr(self, id): 
     157        Person(id=id) 
     158        raise Exception('test') 
     159     
     160def test_exc_rollback(): 
     161    cherrypy.root = RbRoot() 
     162    create_request('/doerr?id=24') 
     163    print cherrypy.response.body[0] 
     164    assert Person.get(24) is None 
     165    assert Person.get(25) is not None 
    148166 
    149167#--