• Post Reply Bookmark Topic Watch Topic
  • New Topic

using java.util.UUID  RSS feed

 
Tushar Kapila
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
required - a UUID to be passed in XML / stored in database.

i saw java 5 has a new class for this.

Wanted to know if my usage was the good:



my question is that is my assumption that the full UUID as a string is :


if i do not Math.abs the LeastSignificantBits then I get a negative number there sometimes. does not look right ... its fine and expected if the complete number is negative at times...


also this UUID takes a few milliesconds to generate and I saw that this class is a little faster:


question - is this method unsafe? can the combination of currentTimeMillis, System.nanoTime() (new in java 1.5) and rnd.nextLong() not be unique?

its for a j2ee app that runs all the time and has quite a few transactions...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're putting it in a DB anyway, why not use a sequence?
 
Tushar Kapila
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
need it in xml before that. just assume that a db is not available when i need to generate , pass this around
 
Gokul Nanthakumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of using a random Unique number like this, if you can generate some unique number according to your xml data (may be by adding time or getting some sequence value from db or some internal sequence maintained) will be better, as it can be used for different purposes like the order of xml generation, etc.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch ( ) Gokul Nanthakumar
 
Tushar Kapila
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gokul Nanthakumar wrote:Instead of using a random Unique number like this, if you can generate some unique number according to your xml data (may be by adding time or getting some sequence value from db or some internal sequence maintained) will be better, as it can be used for different purposes like the order of xml generation, etc.
its for a financial transaction and it must be unique, not in our system alone but in the other parties as well.

i really want to know if the method of using Java's UUID is OK?



 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How on earth can you make a unique identifier that's unique *everywhere*???
 
Tushar Kapila
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:How on earth can you make a unique identifier that's unique *everywhere*???


From java.util.UUID javadoc "A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. "

internally they use a java.security.SecureRandom - which does a lot of nifty things if you ( looked inside src.zip)



 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a namespaced-variant is being used, maybe; I don't believe you could rely on the random-number variant, though (as per the RFC).
 
Tushar Kapila
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:If a namespaced-variant is being used, maybe; I don't believe you could rely on the random-number variant, though (as per the RFC).


by namespaced variant - do you mean - take part of the UUID from my data and append it to the UUID?

psuedo code :

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!