简体   繁体   中英

Algorithm to generate global (Application scoped) unique request id based on HttpServletRequest?

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,

如果你必须绝对确定你没有复制,你可以使用自动增量数据库ID,但鉴于使用UUID重复的机率极低,我不认为它证明了成本。

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 .

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM