Hi,
With TG-1.5 we can now run database transactions as a CP3 tool instead
of @expose. That way you can easily decide which controllers that gets
transactions and who don't by only enabling/disabling the CP3 tool for
specific controllers. I think it would be nice to get transaction
handling away from expose/entangle. So I've written a CP3 tool to
handle transactions. Thus to enable transactions you simply have to
add the Transaction tool to some startup code (commands.py or
startup.py etc):
cherrypy.tools.transactions = TransactionTool()
The tool could be enabled with either:
tools.transactions.on = True
or enabling it in the RootController:
_cp_config = {
'tools.transactions.on' : True,
}
That way it's easy to have fine level control of each exposed
controller and enable/disable transactions where needed. I would like
to contribute this idea to TG-1.5 and think it would be great if we
could get this into place. I've only tried this with SO so the tool
probably need some more code to handle SA as well, but it shouldn't
difficult . Please give me comments and suggestions on how to improve
it and if it's interesting to remove the transactions handling away
from @expose. (I think @expose does to much already). But maybe
there's some side-effects I've forgot about?. Here's the tool:
import cherrypy
from turbogears.database import commit_all, end_all, rollback_all
class TransactionTool(cherrypy.Tool):
"""A TurboGears 1.5 Transaction tool"""
def __init__(self):
return super(TransactionTool,
self).__init__("on_start_resource", self.begin)
def begin(self):
# To make sure TG does not run with transactions
cherrypy.request.in_transaction = True
def _setup(self):
conf = self._merged_args()
p = conf.pop("priority", None)
if p is None:
p = getattr(self.callable, "priority", self._priority)
cherrypy.request.hooks.attach(self._point, self.callable,
priority=p, **conf)
cherrypy.request.hooks.attach('before_error_response',
rollback_all)
# Lower priority for commit to run before TG EndTransactions
hook
cherrypy.request.hooks.attach('on_end_resource', commit_all,
priority=40)
# Not necessary because of TG EndTransactions hook, but
perhaps we could
# remove the hook and use this tool instead?
cherrypy.request.hooks.attach('on_end_request', end_all,
priority=50)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" 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/turbogears-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---