[Zope] Committing transaction by hand

Roché Compaan roche at upfrontsystems.co.za
Sun Aug 8 08:26:14 EDT 2004


* Maciek Pasternacki <maciekp at japhy.fnord.org> [2004-08-08 05:57]:
> I'm writing Zope product supposed to give shopping cart functionality.
> Order can be specified and completed by anonymous user; user should be
> prompted for password in latest possible moment, i.e. when throwing
> order to the cart.  Order is remembered in session variable.  When
> user decides that order is complete I call (from unrestricted code)
> something like:
> 
>     self.Cart().ThrowIn(self.REQUEST)
> 
> Cart() returns instance of `current' shopping cart (one that user is
> currently using); when there is no current cart, one is created; when
> user isn't logged in, Unauthorized is raised.  Cart is not calling
> object's own method but independent callable object acquired by
> calling object.
> 
> Now when user isn't logged in, Cart() raises Unauthorized, user gets
> asked for login and password, and boom!  We have no order data in
> REQUEST.SESSION after authentication.  I read in one of recent posts
> on this list that it's because exception aborts ZODB transaction, so
> I tried to work around the problem by committing transaction before
> calling Cart(), so now our fragment looks like this:
> 
> 
>     get_transaction().commit()
>     self.Cart().ThrowIn(self.REQUEST)
> 
> It seems to work for now but I'm not really sure if such messing with
> something usually done automagically by Zope won't have some obscure
> and nasty side effects.  Am I doing something dangerous now or is this
> the Right Way to deal with my problem and if it's not then what is?

There's nothing wrong with committing a transaction by hand - it can be
very useful at times. Just make sure that you are not committing state
that you actually don't want persisted in cases where exceptions other
than Unauthorized are raised. Or put differently, only commit when the
user is anonymous.

-- 
Roché Compaan
Upfront Systems                 http://www.upfrontsystems.co.za


More information about the Zope mailing list