[ZODB-Dev] Basic ZODB practices
John Belmonte
john at neggie.net
Tue Mar 9 10:33:57 EST 2004
Hector Sanchez SanMartin wrote:
> I'm using ZODB and I need to minimise ConflictErrors in my
> application so I would like to describe my situation and ask for some
> ideas about how could improve it (if possible) in order to minimise
> ConflictErrors as I'm sure you have already faced the same problem.
> So, I have a collection of objects which must be uniquely identified.
> For doing this I'm keeping a simple counter object in the DB which gives
> consecutive ids. These objects need to be retrieved by clients from
> their id so I'm using a OOBTree to hold those objects and keep them in
> the DB, using their id as key.
> My main problem is that the counter object is a concurrency
> bottleneck as for each new object as I need to gather a new id from it,
> thus concurrent counter object modifications provoke a conflict. I could
> certainly solve the conflict using the _p_resolveConflict method but
> this method is only called at commit time and this could provoke that
> two objects get the same id.
> Any suggestion, idea, piece of doc I could refer to in order to have
> some alternative ideas?
The first course is to avoid sequential ID's completely. You can use
Python's rand library to generate a random ID. Before inserting an
object into your collection, verify that you don't have an ID collision.
Collisions should be extremely rare if you use an ID size such as 64
bits. The BTree will also benefit from having random keys, as this will
minimize write conflicts caused by shuffled buckets.
You might also look at ZODB.Connection.Connection.add, which would let
you use ZODB's object ID as your ID. You may get more BTree container
conflicts due to the proximity of inserted keys.
There have also been some ID generator implementations discussed on the
list. The one I've seen requires modifications to ZEO and the file
storage-- not very attractive.
-John
--
http:// if ile.org/
More information about the ZODB-Dev
mailing list