[ZODB-Dev] Multi-Threaded Access
Tres Seaver
tseaver at palladion.com
Fri Jan 29 01:04:25 EST 2010
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jared Smith wrote:
> I'm looking at using ZODB and have two different databases stored in
> different files where different threads will be periodically
> insert/update/delete various records in both databases. Couple of questions
> to make sure this is feasible:
>
> When one of the threads wants to do an insert of a set of records I need to
> ensure that those changes are submitted in a transaction that does not
> overlap with the records being inserted in another thread. Looks like the
> default API assumes there is a single global transaction
> (transaction.commit()) however I saw mention of creating your own
> transaction.manager that you can bind at DB.open() time.
>
> Does this mean to support this model I have to re-open the database and do
> the binding of a transaction manager in every method where I want to do this
> transactional action? For the moment I was assuming a DB access object that
> is shared (singleton access class).
>
> Is the better model to have something like a pool of DB access classes where
> each one maintains a DB handle and a transaction and one of the access
> classes would be taken out of the pool and used every time a transaction
> needs to be run? Worried a bit about the overhead of re-opening the DB is
> this not something to worry about?
This second pattern is the one commonly used in Zope and other
ZODB-based web apps: each request gets a "connetion" object from a pool
maintained by the database, and uses the objects in its cache to satisfy
the request. If the request completes successfully, the application
then commits the transaction, other wise it aborts. In either case it
returns the connection to the pool at the end of the request.
The current transaction for a request is stored in a thread local.
Committing a transaction can fail (raise a ConflictError) if another,
overlapping transaction has modified some of the same objects: in this
case, the application thread has the option of synchronizing its
connection's cache and retrying the request (Zope tries up to three times).
Hope that helps,
Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 tseaver at palladion.com
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAktiemEACgkQ+gerLs4ltQ4jVgCeLUC3nyLPToguuFujL8fe06Jg
8T0AoIR8ShyGrHDiq41DsmGBKgLs+KxV
=Rm/D
-----END PGP SIGNATURE-----
More information about the ZODB-Dev
mailing list