[Twisted-Python] long-lived database transaction

Federico Di Gregorio fog at initd.org
Sat Dec 13 14:15:01 EST 2003


Hi *,

attached to this mail there are two files that implement long-lived
database transactions for twisted (xdbapi.py, to be dropped in
twisted.enterprise) and the necessary unit tests (test_xdbapi.py). 

some months ago i needed database transactions that span more than a
single deferred chain (i.e., runInteraction) and i noting that twisted
was missing such a feature i implemented them in my application. now
that i have some free time, i though it would be nice to let other
people use my code directly from twisted. note that everything you can
do with xdbapi can be done with adbapi and long chains of deferred, but
for some kind of applications keeping around an open transaction and
committing at the end of a complex process is much more simplier.

here is a little example, note that all the methods are completely
asynchronous and return deferreds. also note that the example just show
the usage but not the "reason" for xdbapi. (the original need was that
sometimes you can't commit some information because the whole stuff
comes in pieces but accumulating it makes the code much more complicated
than just writing to the db and the commit the whole at the end.)

class MyAppUsingLongLivedTransactions:

    def __init__(self):
        self.dbpool = xdbapi.ConnectionPool('psycopg', database='test')

    def doSomething(self):
        # react to some user input, open the transaction and save it away,
        # also do a select and present the user some data
        d = self.dbpool.begin()
        d.addCallback(self.doSomething_cb)

    def doSomething_cb(self, trans):
        self.trans = trans
        d = self.trans.runQuery("SELECT ...")
        d.addCallback(self.doSomethingWithTheDataAndAskUserNextStep)

    def doSomthingElse(self, data):
        # the user gave us some data and we need to write to db and commit
        self.trans.runOperation("INSERT...", data)
        # no callback/errback? ouch!
        self.trans.commit()
        # ouch! again, but this is just an example

-- 
Federico Di Gregorio                         http://people.initd.org/fog
Debian GNU/Linux Developer                                fog at debian.org
INIT.D Developer                                           fog at initd.org
  L'unica cosa che riesco a produrre con una certa precisione nella mia
   vita sono i dubbi.                                  -- Natale Titotto
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xdbapi.py
Type: text/x-python
Size: 10079 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031213/2440b483/attachment.py 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_xdbapi.py
Type: text/x-python
Size: 10423 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031213/2440b483/attachment-0001.py 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Questa parte del messaggio =?ISO-8859-1?Q?=E8?= firmata
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031213/2440b483/attachment.pgp 


More information about the Twisted-Python mailing list