1.1 --- a/WebStack/Resources/Selectors.py Sat Dec 01 00:33:16 2007 +0000
1.2 +++ b/WebStack/Resources/Selectors.py Sat Dec 01 00:33:29 2007 +0000
1.3 @@ -99,4 +99,41 @@
1.4 trans.default_charset = self.encoding
1.5 self.resource.respond(trans)
1.6
1.7 +class StoreSelector:
1.8 +
1.9 + """
1.10 + Maintain an attribute for a data store on transactions, performing an
1.11 + automatic rollback after the transaction has finished. The data store can be
1.12 + a DB-API connection object or any object providing a 'rollback' method.
1.13 + """
1.14 +
1.15 + def __init__(self, resource, store):
1.16 +
1.17 + """
1.18 + Initialise the selector with a 'resource' (to which all requests shall
1.19 + be forwarded), providing a 'store' which shall be maintained as an
1.20 + attribute.
1.21 + """
1.22 +
1.23 + self.resource = resource
1.24 + self.store = store
1.25 +
1.26 + def respond(self, trans):
1.27 +
1.28 + """
1.29 + Respond to the transaction 'trans' by setting the "store" attribute to
1.30 + refer to the 'store' attribute on this object, then forwarding the
1.31 + transaction to the previously specified resource.
1.32 +
1.33 + Regardless of how the resource completes its processing, the 'rollback'
1.34 + method of the store will then be called in order to terminate any
1.35 + unfinished, uncommitted transactions.
1.36 + """
1.37 +
1.38 + trans.get_attributes()["store"] = self.store
1.39 + try:
1.40 + self.resource.respond(trans)
1.41 + finally:
1.42 + self.store.rollback()
1.43 +
1.44 # vim: tabstop=4 expandtab shiftwidth=4