[ZODB-Dev] Thread safety

Dmitry Vasiliev lists at hlabs.spb.ru
Tue Oct 5 09:46:14 EDT 2004


Tim Peters wrote:
> [Dmitry Vasiliev]
> 
>>For example consider the following code:
>>
>>def getReply(request):
>>     lock.acquire()
>>     try:
>>         try:
>>             reply = _getReply(request)
>>             transaction.commit()
>>         except:
>>             log.error("error", exc_info=True)
>>             transaction.abort()
>>             reply = None
>>         return reply
>>     finally:
>>         lock.release()
>>
>>All persistent objects changes only inside _getReply() function. Two
>>threads concurrently calls getReply() function...
>>
>>Is it safe? Is it possibly/safe to use only one connection object?
> 
> *Offhand* I can't think of a reason for that breaking.  But it's not how
> threads were intended to be used in ZODB, and I don't have spare time to
> think about it.  Maybe someone else does.

Thanks a lot!

The only problem with the code above that it should use 
transaction.TransactionManager instead of default 
transaction.ThreadTransactionManager.

Quote from ZODB.Connection.Connection docstring talk about this use case:
"""
     Synchronization
     ---------------

     A Connection instance is not thread-safe.  It is designed to
     support a thread model where each thread has its own transaction.
     If an application has more than one thread that uses the
     connection or the transaction the connection is registered with,
     the application should provide locking.
"""

-- 
Dmitry Vasiliev (dima at hlabs.spb.ru)
     http://hlabs.spb.ru


More information about the ZODB-Dev mailing list