You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by "Janssen, Roger" <Ro...@ibanx.nl> on 2003/03/26 13:14:24 UTC

SequenceGenerator might generate incorrect sequence (duplicates)

Hi,
 
i stumbled across this, correct me if i'm wrong;
 
suppose i have two webservers running the same webapp against the same
database. Both webapp instances run in their own jvm, running their own
instance of OJB. Using sequences, the sequence generator instances fetch and
updat the sequences with the new max_key.
 
However, their is no concurrency control! Both webapp instances may end up
with the same sequence instance (same max_key, same currval) if sequence
generators fetched the sequence concurrently at the same time, and thus be
generating the same id's.
 
A solution might be to use locking (set locking='true' in repository.xml) on
the maxKey attribute of the sequence (i.e. only if my copy of the sequence
is the most recent, i may update it). This way, only one of the generator
instances will succeed in updating the sequence, the others will fail. Some
exception handling should be introduced where concurrentmodifications are
caught. Instead of directly failing, within a loop several retries could be
issued (fetch new instances and try to update it again) so the getUniqueId
method of a sequencemanager may still succeed. Only if even after several
retries, we were not able to successfully (==correctly) update the sequence,
getting a correct new id, an exception should be thrown.
 
 


*************************************************************************
The information contained in this communication is confidential and is
intended solely for the use of the individual or entity to  whom it is
addressed.You should not copy, disclose or distribute this communication 
without the authority of iBanx bv. iBanx bv is neither liable for 
the proper and complete transmission of the information has been maintained
nor that the communication is free of viruses, interceptions or interference.

If you are not the intended recipient of this communication please return
the communication to the sender and delete and destroy all copies.

Re: SequenceGenerator might generate incorrect sequence (duplicates)

Posted by Armin Waibel <ar...@code-au-lait.de>.
Hi Roger,

you are right, it's a known problem.
I'm currently on that stuff.

> A solution might be to use locking (set locking='true' in
repository.xml) on
> the maxKey attribute of the sequence (i.e. only if my copy of the
sequence
> is the most recent, i may update it). This way, only one of the
generator
> instances will succeed in updating the sequence, the others will fail.
Currently locking is a ODMG-api feature, thus it does not help. Further
on to make 'safe locks with different JVM' you have to use the
persistent
locking mode, this will slow down performance.

It's recommended to use database based sequence generation
when using different JVM. Dependend from the the used
database you could use SequenceManagerNextValImpl
as sequence manager in your jdbc-conncetion-descriptor.

Which database do you use?

regards,
Armin

----- Original Message -----
From: "Janssen, Roger" <Ro...@ibanx.nl>
To: "OJB Users List (E-mail)" <oj...@db.apache.org>
Sent: Wednesday, March 26, 2003 1:14 PM
Subject: SequenceGenerator might generate incorrect sequence
(duplicates)


> Hi,
>
> i stumbled across this, correct me if i'm wrong;
>
> suppose i have two webservers running the same webapp against the same
> database. Both webapp instances run in their own jvm, running their
own
> instance of OJB. Using sequences, the sequence generator instances
fetch and
> updat the sequences with the new max_key.
>
> However, their is no concurrency control! Both webapp instances may
end up
> with the same sequence instance (same max_key, same currval) if
sequence
> generators fetched the sequence concurrently at the same time, and
thus be
> generating the same id's.
>
> A solution might be to use locking (set locking='true' in
repository.xml) on
> the maxKey attribute of the sequence (i.e. only if my copy of the
sequence
> is the most recent, i may update it). This way, only one of the
generator
> instances will succeed in updating the sequence, the others will fail.
Some
> exception handling should be introduced where concurrentmodifications
are
> caught. Instead of directly failing, within a loop several retries
could be
> issued (fetch new instances and try to update it again) so the
getUniqueId
> method of a sequencemanager may still succeed. Only if even after
several
> retries, we were not able to successfully (==correctly) update the
sequence,
> getting a correct new id, an exception should be thrown.
>
>
>
>
>
************************************************************************
*
> The information contained in this communication is confidential and is
> intended solely for the use of the individual or entity to  whom it is
> addressed.You should not copy, disclose or distribute this
communication
> without the authority of iBanx bv. iBanx bv is neither liable for
> the proper and complete transmission of the information has been
maintained
> nor that the communication is free of viruses, interceptions or
interference.
>
> If you are not the intended recipient of this communication please
return
> the communication to the sender and delete and destroy all copies.
>