Reputation: 9655
I have issue with generating global unique request id based on HttpServletRequest. I need to make sure the generated id is unique in Application scope. Java UUID Or Random does not guarantee what I want although the chance for duplicate is very low.
Anyone has any ideas? Thanks,
Upvotes: 0
Views: 649
Reputation: 1109635
Random
definitely doesn't guarantee uniqueness, but UUID
definitely does that. So your concern makes no sense. Even if you've hardware capable of serving a billion HTTP requests per second, you'd only after 100 years reach 50% chance on a duplicate.
If you have really really a hard head in this, just check in synchronized block if the application scope contains already such a key and if so, then just generate another one. And so on. This way you can even use Random
.
Upvotes: 1
Reputation: 21
If you have to be absolutely sure you don`t make a duplicate you could use an auto increment database id, but given the incredibly low chances of duplicates using UUID i dont think it justifies the cost.
Upvotes: 1