Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   IBM-recommended method not thread-safe? (http://www.velocityreviews.com/forums/t390824-ibm-recommended-method-not-thread-safe.html)

Larry 02-13-2007 06:11 PM

IBM-recommended method not thread-safe?
 
On the following page:

http://www-128.ibm.com/developerwork...rticle/dm-0407...

IBM discusses a database "surrogate key" generation function, along
with a
listing in Java (Appendix A at the end of the web page). However it
seems the function is not thread-safe!

Suppose for a certain Table X, the SURROGATEKEYVALUE is currently set
to 100, and INCREMENT is set to 1. If 2 threads, A and B, enter
getSurrogateKey at nearly the same time It is possible for the
following sequence to occur:

1. Thread A executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 101.

2. Thread B executes:
updateKeyStmt.execute();
As a result, SURROGATEKEYVALUE is now set to 102.

3. Thread A executes:
ResultSet rs = getKeyStmt.executeQuery();

This will return 102.

4. Thread B executes:
ResultSet rs = getKeyStmt.executeQuery();

This will also return 102 !

Am I missing something here?


Daniel Pitts 02-13-2007 08:38 PM

Re: IBM-recommended method not thread-safe?
 
On Feb 13, 10:11 am, "Larry" <larry.grant...@gmail.com> wrote:
> On the following page:
>
> http://www-128.ibm.com/developerwork...rticle/dm-0407...
>
> IBM discusses a database "surrogate key" generation function, along
> with a
> listing in Java (Appendix A at the end of the web page). However it
> seems the function is not thread-safe!
>
> Suppose for a certain Table X, the SURROGATEKEYVALUE is currently set
> to 100, and INCREMENT is set to 1. If 2 threads, A and B, enter
> getSurrogateKey at nearly the same time It is possible for the
> following sequence to occur:
>
> 1. Thread A executes:
> updateKeyStmt.execute();
> As a result, SURROGATEKEYVALUE is now set to 101.
>
> 2. Thread B executes:
> updateKeyStmt.execute();
> As a result, SURROGATEKEYVALUE is now set to 102.
>
> 3. Thread A executes:
> ResultSet rs = getKeyStmt.executeQuery();
>
> This will return 102.
>
> 4. Thread B executes:
> ResultSet rs = getKeyStmt.executeQuery();
>
> This will also return 102 !
>
> Am I missing something here?


You need to use an atomic query that updates the data at the same time
as returning the value.


Lew 02-13-2007 10:55 PM

Re: IBM-recommended method not thread-safe?
 
"Larry" <larry.grant...@gmail.com> wrote:
>> On the following page:
>>
>> http://www-128.ibm.com/developerwork...rticle/dm-0407...
>>
>> IBM discusses a database "surrogate key" generation function, along
>> with a
>> listing in Java (Appendix A at the end of the web page). However it
>> seems the function is not thread-safe!
>>
>> Suppose for a certain Table X, the SURROGATEKEYVALUE is currently set
>> to 100, and INCREMENT is set to 1. If 2 threads, A and B, enter
>> getSurrogateKey at nearly the same time It is possible for the
>> following sequence to occur:
>>
>> 1. Thread A executes:
>> updateKeyStmt.execute();
>> As a result, SURROGATEKEYVALUE is now set to 101.
>>
>> 2. Thread B executes:
>> updateKeyStmt.execute();
>> As a result, SURROGATEKEYVALUE is now set to 102.
>>
>> 3. Thread A executes:
>> ResultSet rs = getKeyStmt.executeQuery();
>>
>> This will return 102.
>>
>> 4. Thread B executes:
>> ResultSet rs = getKeyStmt.executeQuery();
>>
>> This will also return 102 !
>>
>> Am I missing something here?


Daniel Pitts wrote:
> You need to use an atomic query that updates the data at the same time
> as returning the value.


The problem is likely database concurrency rather than thread concurrency. Try
wrapping your transactions in COMMIT / ROLLBACK.

- Lew

=?ISO-8859-1?Q?Arne_Vajh=F8j?= 02-14-2007 12:18 AM

Re: IBM-recommended method not thread-safe?
 
Larry wrote:
> On the following page:
>
> http://www-128.ibm.com/developerwork...rticle/dm-0407...
>
> IBM discusses a database "surrogate key" generation function, along
> with a
> listing in Java (Appendix A at the end of the web page). However it
> seems the function is not thread-safe!


> Am I missing something here?


No - you are correct.

The code is not thread safe as is.

To do that getSurrogateKey should be made synchronized
to be threadsafe in a single node config and use some
database synchronization technique like a transaction
with transaction islation level serializable or switch
the statements and use SELECT ... FOR UPDATE (I can not
remember if DB2 supports that) to be safe in a multi node
config.

But if you look at the Singleton method, then you
get a clear indication that there is something very wrong
with that code.

Arne

Larry 02-14-2007 02:15 AM

Re: IBM-recommended method not thread-safe?
 
On Feb 13, 7:18 pm, Arne Vajh°j <a...@vajhoej.dk> wrote:
> Larry wrote:
> > On the following page:

>
> >http://www-128.ibm.com/developerwork...rticle/dm-0407...

>
> > IBM discusses a database "surrogate key" generation function, along
> > with a
> > listing in Java (Appendix A at the end of the web page). However it
> > seems the function is not thread-safe!
> > Am I missing something here?

>
> No - you are correct.
>
> The code is not thread safe as is.
>


So what is it doing on an official IBM site?


=?ISO-8859-1?Q?Arne_Vajh=F8j?= 02-14-2007 02:57 AM

Re: IBM-recommended method not thread-safe?
 
Larry wrote:
> On Feb 13, 7:18 pm, Arne Vajh°j <a...@vajhoej.dk> wrote:
>> Larry wrote:
>>> On the following page:
>>> http://www-128.ibm.com/developerwork...rticle/dm-0407...
>>> IBM discusses a database "surrogate key" generation function, along
>>> with a
>>> listing in Java (Appendix A at the end of the web page). However it
>>> seems the function is not thread-safe!
>>> Am I missing something here?

>> No - you are correct.
>>
>> The code is not thread safe as is.

>
> So what is it doing on an official IBM site?


I do not know.

Maybe the article is written by a DB2 expert
not a Java expert.

Maybe an early draft of the code got posted instead
of the final.

Mistakes happen in IBM also.

Arne

Larry 02-14-2007 06:28 PM

Re: IBM-recommended method not thread-safe?
 
On Feb 13, 9:57 pm, Arne Vajh°j <a...@vajhoej.dk> wrote:
> Larry wrote:
> > On Feb 13, 7:18 pm, Arne Vajh°j <a...@vajhoej.dk> wrote:
> >> Larry wrote:
> >>> On the following page:
> >>>http://www-128.ibm.com/developerwork...rticle/dm-0407....
> >>> IBM discusses a database "surrogate key" generation function, along
> >>> with a
> >>> listing in Java (Appendix A at the end of the web page). However it
> >>> seems the function is not thread-safe!
> >>> Am I missing something here?
> >> No - you are correct.

>
> >> The code is not thread safe as is.

>
> > So what is it doing on an official IBM site?

>
> I do not know.
>
> Maybe the article is written by a DB2 expert
> not a Java expert.
>
> Maybe an early draft of the code got posted instead
> of the final.
>
> Mistakes happen in IBM also.
>
> Arne



The feedback page lists Java and EJB among the author's specialties.
In any event, I have emailed my concerns to the author and also left
feedback on the page. It seems that the page was put up in 2004, so
it's still strange that the error is still there.


Chris Uppal 02-14-2007 07:15 PM

Re: IBM-recommended method not thread-safe?
 
Larry wrote:

> The feedback page lists Java and EJB among the author's specialties.
> In any event, I have emailed my concerns to the author and also left
> feedback on the page. It seems that the page was put up in 2004, so
> it's still strange that the error is still there.


Maybe it's not ;-)

When I try to visit:

http://www-128.ibm.com/developerwork...rticle/dm-0407

I get an error:

Our apologies...
The page you requested cannot be displayed

(both today, and when the OP originally mentioned it).

-- chris



=?ISO-8859-1?Q?Arne_Vajh=F8j?= 02-15-2007 12:04 AM

Re: IBM-recommended method not thread-safe?
 
Chris Uppal wrote:
> Larry wrote:
>> The feedback page lists Java and EJB among the author's specialties.
>> In any event, I have emailed my concerns to the author and also left
>> feedback on the page. It seems that the page was put up in 2004, so
>> it's still strange that the error is still there.

>
> Maybe it's not ;-)
>
> When I try to visit:
>
> http://www-128.ibm.com/developerwork...rticle/dm-0407
>
> I get an error:
>
> Our apologies...
> The page you requested cannot be displayed
>
> (both today, and when the OP originally mentioned it).


That is just because the URL was cut short in the original post.

The correct URL is:

http://www-128.ibm.com/developerwork...ang/index.html

(I hope it goes through without being truncated)

Arne

Chris Uppal 02-15-2007 06:17 PM

Re: IBM-recommended method not thread-safe?
 
Arne Vajh°j wrote:

> > http://www-128.ibm.com/developerwork...rticle/dm-0407

[...]

> That is just because the URL was cut short in the original post.
>
> The correct URL is:
>
>

http://www-128.ibm.com/developerwork...ang/index.html

Thank you.

But how did you manage to guess the full URL ? Other people seemed to be able
to read the article too, but while it's common enough to add a missing .html or
index.htm to the end of an URL, in this case the missing bit was

zhang/index.html

Am I alone in lacking the clairvoyant skills needed to reconstruct it ?

-- chris




All times are GMT. The time now is 12:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.