Trying ZODB, background in Relational: mimic auto_increment?

Diez B. Roggisch deets at nospam.web.de
Thu Aug 14 13:48:25 EDT 2008


Jean-Paul Calderone schrieb:
> On Thu, 14 Aug 2008 16:15:11 +0200, "Diez B. Roggisch" 
> <deets at nospam.web.de> wrote:
>> Jean-Paul Calderone wrote:
>>
>>> On Thu, 14 Aug 2008 05:22:35 -0700 (PDT), Phillip B Oldham
>>> <phillip.oldham at gmail.com> wrote:
>>>> [snip]
>>>>
>>>> How would one assign a unique ID to the root at that point?
>>>
>>> Here's one way
>>>
>>>     class Sequence(Persistence):
>>>         def __init__(self):
>>>             self.current = 0
>>>
>>>         def next(self):
>>>             self.current += 1
>>>             return self.current
>>>
>>>     ticketSequence = Sequence()
>>>
>>>     class Ticket(Persistence):
>>>         def __init__(self):
>>>             self.id = ticketSequence.next()
>>
>> Be aware that this isn't working concurrently. Depending on your
>> application, this can be mitigated using a simple threading.Lock.
>>
> 
> ZODB has transactions.  That's probably the best way to make this
> code safe for concurrent use.

But that's a great deal more complicated! It requires merging, and while 
this of course is possible, you should have mentioned it because 
otherwise the OP might run into problems.

Diez



More information about the Python-list mailing list