IBM-recommended method not thread-safe?

Discussion in 'Java' started by Larry, Feb 13, 2007.

  1. Larry

    Larry Guest

    On the following page:

    http://www-128.ibm.com/developerworks/db2/library/techarticle/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?
     
    Larry, Feb 13, 2007
    #1
    1. Advertising

  2. Larry

    Daniel Pitts Guest

    On Feb 13, 10:11 am, "Larry" <> wrote:
    > On the following page:
    >
    > http://www-128.ibm.com/developerworks/db2/library/techarticle/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.
     
    Daniel Pitts, Feb 13, 2007
    #2
    1. Advertising

  3. Larry

    Lew Guest

    "Larry" <> wrote:
    >> On the following page:
    >>
    >> http://www-128.ibm.com/developerworks/db2/library/techarticle/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
     
    Lew, Feb 13, 2007
    #3
  4. Larry wrote:
    > On the following page:
    >
    > http://www-128.ibm.com/developerworks/db2/library/techarticle/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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 14, 2007
    #4
  5. Larry

    Larry Guest

    On Feb 13, 7:18 pm, Arne Vajhøj <> wrote:
    > Larry wrote:
    > > On the following page:

    >
    > >http://www-128.ibm.com/developerworks/db2/library/techarticle/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?
     
    Larry, Feb 14, 2007
    #5
  6. Larry wrote:
    > On Feb 13, 7:18 pm, Arne Vajhøj <> wrote:
    >> Larry wrote:
    >>> On the following page:
    >>> http://www-128.ibm.com/developerworks/db2/library/techarticle/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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 14, 2007
    #6
  7. Larry

    Larry Guest

    On Feb 13, 9:57 pm, Arne Vajhøj <> wrote:
    > Larry wrote:
    > > On Feb 13, 7:18 pm, Arne Vajhøj <> wrote:
    > >> Larry wrote:
    > >>> On the following page:
    > >>>http://www-128.ibm.com/developerworks/db2/library/techarticle/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.
     
    Larry, Feb 14, 2007
    #7
  8. Larry

    Chris Uppal Guest

    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/developerworks/db2/library/techarticle/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
     
    Chris Uppal, Feb 14, 2007
    #8
  9. 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/developerworks/db2/library/techarticle/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/developerworks/db2/library/techarticle/dm-0407zhang/index.html

    (I hope it goes through without being truncated)

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 15, 2007
    #9
  10. Larry

    Chris Uppal Guest

    Arne Vajhøj wrote:

    > > http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0407

    [...]

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

    http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0407zhang/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
     
    Chris Uppal, Feb 15, 2007
    #10
  11. Chris Uppal wrote:
    > Arne Vajhøj wrote:
    > 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 ?


    I Googled on:

    surrogate key +site:ibm.com

    (the term "surrogate key" were in the original post)

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 16, 2007
    #11
  12. Larry

    Chris Uppal Guest

    Arne Vajhøj wrote:

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

    >
    > I Googled on:
    >
    > surrogate key +site:ibm.com
    >
    > (the term "surrogate key" were in the original post)


    How disappointing ! You mean you didn't sacrifice even /one/ black goat ?

    -- chris
     
    Chris Uppal, Feb 16, 2007
    #12
  13. Chris Uppal wrote:
    > Arne Vajhøj wrote:
    >>> Am I alone in lacking the clairvoyant skills needed to reconstruct it ?

    >> I Googled on:
    >>
    >> surrogate key +site:ibm.com

    >
    > How disappointing ! You mean you didn't sacrifice even /one/ black goat ?


    I not even crossed my fingers when I hit the Google Search button.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Feb 17, 2007
    #13
  14. Larry

    Chris Uppal Guest

    Arne Vajhøj wrote:

    [me:]
    > > How disappointing ! You mean you didn't sacrifice even /one/ black
    > > goat ?

    >
    > I not even crossed my fingers when I hit the Google Search button.


    :)

    -- chris
     
    Chris Uppal, Feb 17, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruno Lienard
    Replies:
    0
    Views:
    331
    Bruno Lienard
    Oct 25, 2004
  2. Robert Wells
    Replies:
    4
    Views:
    666
    Default User
    Jun 24, 2008
  3. Gabriel Rossetti
    Replies:
    0
    Views:
    1,330
    Gabriel Rossetti
    Aug 29, 2008
  4. Qu0ll
    Replies:
    42
    Views:
    1,254
    Thufir Hawat
    Apr 13, 2009
  5. John Nagle
    Replies:
    5
    Views:
    475
    John Nagle
    Mar 12, 2012
Loading...

Share This Page