[Zope] Zope and Threads

Johan Carlsson johanc@easypublisher.com
Fri, 11 Jul 2003 13:01:41 +0200


Dieter Maurer wrote:
 > alan runyan wrote at 2003-7-9 13:06 -0500:
 >  > Just wanted to post this for whoever searches for Zope,
 >  > Threads and ZSQLMethods or relational databases in the future.  It 
seems
 >  > that very simplistic things will work
 >  > if you spawn a thread inside of Zope.  But in the thread
 >  > if you start doing anything really related to ZODB
 >  > you could have all sorts of issues.
 >
 > The mailing list archives have some threads about this...
 >

Just checking that I understand the whole thing correctly?

 > There are a few issues you must observe:
 >
 >      In general you must not share a ZODB connection between
 >      threads. This means, you must not pass persistent objects
 >      between threads (as they contain a hidden reference to
 >      a ZODB connection (_p_jar) and may use it non-deterministically).
 >
 >      Open a new ZODB connection in the new thread.
 >      Its "root()" method returns the ZODB root object.


I guess it would look something like this?

import Globals
db=Globals.DB
connection=db.open()
root = connection.root()


 >      Pass an object's path between threads rather than the object
 >      itself. Use "[un]restrictedTraverse" to locate the object
 >      from the root given the path.
 >
 >      Do not forget to commit/abort the (implicitly) started
 >      transaction.

Using get_transaction().commit() and get_transaction().abort()
as usual I guess?

 >      Do not forget to close the opened ZODB connection

With: connection.close()

What about connection.sync(), I would think it syncs commited
data fin other threads back to this connection?
In other words I need to use this in my thread to get an updated
view of the database.
Is this correct?

Johan

-- 
Johan Carlsson          Tel: + 46 8 31 24 94
Colliberty              Mob: + 46 70 558 25 24
Torsgatan 72            Email: johanc@easypublisher.com
SE-113 37 STOCKHOLM