Crap I've been using Pylons for six months now and didn't know about @transactional. I've been writing it out for each controller method.
Duh. On 5/11/07, Michael Bayer <[EMAIL PROTECTED]> wrote: > > > > On May 11, 2007, at 2:48 PM, Paul Johnston wrote: > > > > > Hi, > > > > I've just been looking at how TurboGears uses SQLAlchemy. It surrounds > > each request in a transaction. I just wondered if this has much > > benefit > > for for an ORM that uses the unit-of-work pattern. I have a feeling > > it's > > a hangover from SQLObject support where using a transaction was a good > > pattern. I'd have thought it's enough to just use SessionContext, > > and to > > flush the current session at the end of each request. > > > > How does Pylons do it? > > > > Pylons doesnt wrap requests in transactions for you, and i dont think > thats something that should be happening in all cases. In Pylons I > made a simple decorator to attach "transactional" state to any > controller method, along the lines of: > > def transactional(func): > def do_in_trans(*args, **kwargs): > try: > trans = session_context.current.create_transaction() > response = func(*args, **kwargs) > trans.commit() > return response > except: > trans.rollback() > raise > return do_in_trans > > as an additional tweak, I made it so the controller methods return > their response as "lambda: response", so that the transactional > decorator can "execute" the response (which is typically a template > render operation) after the commit succeeds. > > also pylons uses a non-engine-bound MetaData, and a global > SessionContext where the configured Engine is bound to the current > thread's Session when the "current" attribute is first referenced. I > think its a really clean approach....but of course any operations > directly with SQL have to be executed via the explicit engine (which > I usually get as context.current.bind_to ). > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---
