# HG changeset patch # User paulb # Date 1196469209 0 # Node ID 94d0e64c5c66552f6bc893f2ba314a233b646093 # Parent 05f30cae9f0e82853b09d436d03234c0dbc0282c [project @ 2007-12-01 00:33:29 by paulb] Added a StoreSelector for database-related applications. diff -r 05f30cae9f0e -r 94d0e64c5c66 WebStack/Resources/Selectors.py --- a/WebStack/Resources/Selectors.py Sat Dec 01 00:33:16 2007 +0000 +++ b/WebStack/Resources/Selectors.py Sat Dec 01 00:33:29 2007 +0000 @@ -99,4 +99,41 @@ trans.default_charset = self.encoding self.resource.respond(trans) +class StoreSelector: + + """ + Maintain an attribute for a data store on transactions, performing an + automatic rollback after the transaction has finished. The data store can be + a DB-API connection object or any object providing a 'rollback' method. + """ + + def __init__(self, resource, store): + + """ + Initialise the selector with a 'resource' (to which all requests shall + be forwarded), providing a 'store' which shall be maintained as an + attribute. + """ + + self.resource = resource + self.store = store + + def respond(self, trans): + + """ + Respond to the transaction 'trans' by setting the "store" attribute to + refer to the 'store' attribute on this object, then forwarding the + transaction to the previously specified resource. + + Regardless of how the resource completes its processing, the 'rollback' + method of the store will then be called in order to terminate any + unfinished, uncommitted transactions. + """ + + trans.get_attributes()["store"] = self.store + try: + self.resource.respond(trans) + finally: + self.store.rollback() + # vim: tabstop=4 expandtab shiftwidth=4