Can we generate Unique ID from Java?

M

Max

Hi all,

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

Thanks & regards,
Max.
 
M

Mark Thornton

Max said:
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
 
S

Sudsy

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?
 
M

Mark Thornton

Sudsy said:
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
 
J

Jayaram

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
 
S

Sudsy

Jayaram said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top