CherryPy Project Download

Instead of doing a commit on every query in every action you can allow the request handler to do the commit for you. This is very similar to the Dejavu example shown in the tools section as well. This basically attempts to do a commit and if it doesn't commit it does a rollback and spits out an error.

def do_commit():
	try:
		cherrypy.thread_data.store.commit()
	except:
		cherrypy.thread_data.store.rollback()
		cherrypy.log(format_exc(), "STORM")
		

def try_commit():
	
	if cherrypy.response.stream:
		cherrypy.request.hooks.attach('on_end_request', do_commit)
	else:
		if isinstance(cherrypy.response.body, types.GeneratorType):
			cherrypy.response.collapse_body()
		do_commit()
	
cherrypy.tools.storm = cherrypy.Tool('on_end_resource', try_commit)

Simply create the tool and then in your config you need to have

tools.storm.on = True

You can then create a tool that will attempt to automatically commit your changes to the database on every request.

## The database commit tool. This will try to auto-commit on each request.
def do_commit():
	try:
		cherrypy.thread_data.store.commit()
	except:
		cherrypy.thread_data.store.rollback()
		cherrypy.log(format_exc(), "STORM")
		

def try_commit():
	
	if cherrypy.response.stream:
		cherrypy.request.hooks.attach('on_end_request', do_commit)
	else:
		if isinstance(cherrypy.response.body, types.GeneratorType):
			cherrypy.response.collapse_body()
		do_commit()
	
cherrypy.tools.storm = cherrypy.Tool('on_end_resource', try_commit)

In your config file you will need something like the following to turn it on.

tools.storm.on = True

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages