[Zope-dev] thread locking and ZODB
Chris Withers
chrisw@nipltd.com
Wed, 12 Sep 2001 08:25:36 +0100
Terry Kerr wrote:
>
> I have a method which increments an attribute on the current object. I
> have created a thread lock so that multiple threads accessing the method
> simultaneously cannot stuff up the attribute value:
The ZODB doesn't work like that ;-)
As I understand it, each DB connection gets it's own consistent copy of the ODB,
so you don't need to worry about locking...
> def nextOrderNumber(self):
> lock.acquire()
> self._order_number = self._order_number + 1
> lock.release()
> return str(self._order_number)
>
> My concern is that even though a second thread is halted at
> "lock.acqure()" while the first thread updates the order_number, that
> the second thread has an 'out of date' version of 'self' with the
> original order_number value in it, hence, is still returning the
> incorrect order_number.
Indeed. The is called conflict resolution and your problem is solved as an
example in:
http://www.zope.org/Documentation/Articles/ZODB2
...in the section "Resolving Conflicts"
Personally, I'd use a file-based counter or auto incrementing sequel column.
cheers,
Chris