Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > IBM-recommended method not thread-safe?

Reply
Thread Tools

IBM-recommended method not thread-safe?

 
 
Larry
Guest
Posts: n/a
 
      02-13-2007
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?

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      02-13-2007
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.

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-13-2007
"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
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      02-14-2007
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?

 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      02-14-2007
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
 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      02-14-2007
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.

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-14-2007
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


 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      02-15-2007
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
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-15-2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dumb question: in documentation, why Object#method, and not Object.method ? Elf M. Sternberg Ruby 15 07-29-2009 01:20 AM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
[RCR] Module#nesting (NOT the class method - instance method) ara.t.howard@noaa.gov Ruby 1 02-05-2007 10:53 PM
can call a method, but not a method within a user-defined class aidy Ruby 5 06-04-2006 08:10 PM
invoke a method by reflection£¬the method's parameters can not be ArrayList? jerry051 ASP .Net 2 08-02-2005 10:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57