[Twisted-Python] twisted.enterprise / transactions

Itamar Shtull-Trauring twisted at itamarst.org
Mon Feb 11 12:28:08 EST 2002

Bob Ippolito wrote:

> It seems that enterprise expects that you want to commit data for every 
> successful query?  This certainly makes things simpler, as with 
> PostgreSQL a separate backend (connection) is going to be running for 
> every connection to that database, which means the thread pool would 
> require some more complicated management.  There ought to be a way to 
> set it up such that your callbacks can do more processing and guarantee 
> that it happens in the same thread, so you get the same connection, and 
> don't have to commit every time.  Committing after every query kinda 
> defeats the purpose of having transactions at all, unless you do 
> everything in user defined functions (stored procedures) that use 
> transactions themselves.

There is support for doing so.

Specifically, adabpi.ConnectionPool.interaction() lets you pass a 
callable object  (a function or method). The method you pass will be 
called with an instance of adabpi.Transaction, upon which you can run a 
series of SQL commands, after which the changes will be commited 
automatically (or rollbacked if there's an exception thrown.)

So, write a function that accepts one argument, an instance of 
adbapi.Transaction, and does a series of SQL commands on this instance. 
Then, pass this function to your connectionpoool's interaction() method.

> One more thing, do you need to do anything with threadable before using 
> enterprise?

Nope, it will automatically do that for you, on the assumption 
enterprise.adbapi is imported before your program starts, i.e. before 
you call your Application's run() method (or t.i.main.run()).

More information about the Twisted-Python mailing list