Can we generate Unique ID from Java?

Discussion in 'Java' started by Max, Feb 28, 2004.

  1. Max

    Max Guest

    Hi all,

    I'm thinking to generate unique id for my database primary key. How can we
    do that in Java??

    Thanks & regards,
    Max.
     
    Max, Feb 28, 2004
    #1
    1. Advertising

  2. Max wrote:
    > Hi all,
    >
    > I'm thinking to generate unique id for my database primary key. How can we
    > do that in Java??
    >
    > Thanks & regards,
    > Max.
    >
    >
    >


    There is a java.util.UUID class in 1.5 --- only in beta at the moment.
    There are other classes in current Java under the java.rmi stuff, but
    they make assumptions that are not necessarily true (I have a test
    example that will generate duplicate keys on a sufficiently fast machine).

    Mark Thornton
     
    Mark Thornton, Feb 28, 2004
    #2
    1. Advertising

  3. Max

    Sudsy Guest

    Mark Thornton wrote:
    > Max wrote:
    >
    >> Hi all,
    >>
    >> I'm thinking to generate unique id for my database primary key. How
    >> can we
    >> do that in Java??

    <snip>
    >
    > There is a java.util.UUID class in 1.5 --- only in beta at the moment.
    > There are other classes in current Java under the java.rmi stuff, but
    > they make assumptions that are not necessarily true (I have a test
    > example that will generate duplicate keys on a sufficiently fast machine).


    Mark,
    Sounds like a broken implementation. Does it claim to be ISO/IEC
    11578:1996 compliant? The standard incorporates a random number,
    something which should preclude the collisions you've observed.
    compliant?
     
    Sudsy, Feb 28, 2004
    #3
  4. Sudsy wrote:

    > Mark Thornton wrote:
    >
    >> Max wrote:
    >>
    >>> Hi all,
    >>>
    >>> I'm thinking to generate unique id for my database primary key. How
    >>> can we
    >>> do that in Java??

    >
    > <snip>
    >
    >>
    >> There is a java.util.UUID class in 1.5 --- only in beta at the moment.
    >> There are other classes in current Java under the java.rmi stuff, but
    >> they make assumptions that are not necessarily true (I have a test
    >> example that will generate duplicate keys on a sufficiently fast
    >> machine).

    >
    >
    > Mark,
    > Sounds like a broken implementation. Does it claim to be ISO/IEC
    > 11578:1996 compliant? The standard incorporates a random number,
    > something which should preclude the collisions you've observed.
    > compliant?


    The new UUID class in 1.5 works properly --- it is a standard random
    number based implementation.
    The somewhat broken stuff are the classes java.rmi.dgc.VMID,
    java.rmi.server.UID and related objects. These really require that you
    have only one VM on a given machine and that you are using unique public
    IP addresses (and don't have lots of machines with local addresses like
    192.168.0.1 all communicating via NATing devices).
    I found that it was quite easy on my work machine to start two JVM at
    exactly the same time (as measured by System.currentTimeMillis). Both
    JVM are then likely to generate exactly the same sequence of UID values.
    For the intended use in server type applications these defficiencies are
    probably unimportant. However I wanted to generate ids in client
    applications where having multiple JVM instances would be far more
    likely as would the prospect of using private IP address ranges. In this
    case the probability of generating identical ids is unacceptably high.

    Mark Thornton
     
    Mark Thornton, Feb 28, 2004
    #4
  5. Max

    Jayaram Guest

    Max,
    Define a database sequence. Every time you insert a record into this
    table do something like:
    insert into <your table> (<primary key column name>, <other columns>
    .... ) values (<database sequence name>.nextval, <other column values>
    )
    <database sequence name>.nextval generates a unique key every time you
    call it.
    This way you dont need to worry about generating a unique key at the
    JAVA end - let the database handle it for you.
    Regards,
    Jayaram

    "Max" <> wrote in message news:<c1pfvc$1kjaie$-berlin.de>...
    > Hi all,
    >
    > I'm thinking to generate unique id for my database primary key. How can we
    > do that in Java??
    >
    > Thanks & regards,
    > Max.
     
    Jayaram, Feb 28, 2004
    #5
  6. Max

    Sudsy Guest

    Jayaram wrote:
    > Max,
    > Define a database sequence. Every time you insert a record into this
    > table do something like:
    > insert into <your table> (<primary key column name>, <other columns>
    > ... ) values (<database sequence name>.nextval, <other column values>
    > )
    > <database sequence name>.nextval generates a unique key every time you
    > call it.
    > This way you dont need to worry about generating a unique key at the
    > JAVA end - let the database handle it for you.
    > Regards,
    > Jayaram


    While your suggestion is laudable, it should be noted that the
    mechanism is database-dependant. Not a problem if you use an
    abstraction layer (perhaps DAO) but if you incorporate JDBC
    calls directly into your code then you might cause yourself
    grief. Just a word to the wise.
     
    Sudsy, Feb 28, 2004
    #6
    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. Mullin
    Replies:
    3
    Views:
    5,527
  2. Sanjeev
    Replies:
    3
    Views:
    1,496
    Amit Jain
    Jul 11, 2008
  3. ToshiBoy
    Replies:
    6
    Views:
    875
    ToshiBoy
    Aug 12, 2008
  4. Casey Hawthorne
    Replies:
    1
    Views:
    738
    Arne Vajhøj
    Mar 18, 2009
  5. Token Type
    Replies:
    9
    Views:
    383
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page