java 使用 UUID 生成唯一 ID 真的是唯一的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5728205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is unique id generation using UUID really unique?
提问by vijayasankarj
I want generate unique ID just like auto increment in java . So previously i used current nano seconds but i end up with clash since two data comes with in same nano seconds .. Does UUID solves the above problem ?
我想像 java 中的自动增量一样生成唯一 ID。所以以前我使用了当前的纳秒但我最终会发生冲突,因为两个数据在相同的纳秒内出现.. UUID 是否解决了上述问题?
Note :: In my project i can even get 10000 rows of records for each and every minute and I will dump those records along with UIDS in to table .And there may be a situation where i would stop my product and restart it after some time ....So during that situation how could UUID class clarifies the previously generated Uids(which i stored in DB) with the new one going to created(Yet to be dumped in DB) ?
注意 :: 在我的项目中,我什至可以每分钟获得 10000 行记录,我会将这些记录与 UIDS 一起转储到表中。而且可能会出现这样的情况,我会停止我的产品并在一段时间后重新启动它....那么在这种情况下,UUID 类如何用将要创建的新 UID(尚未转储到 DB)来澄清先前生成的 Uid(我存储在 DB 中)?
回答by AbdullahC
While the UUIDs are not guaranteedto be unique, the probability of a duplicate is extremely low. See Random UUID probability of duplicates.
虽然不能保证UUID 是唯一的,但重复的概率极低。请参阅重复的随机 UUID 概率。
For your application, it makes sense to use the UUID, but you may want to deal with the extremely rare condition, just in case.
对于您的应用程序,使用 UUID 是有意义的,但您可能希望处理极其罕见的情况,以防万一。
回答by Peter Lawrey
I seriously doubt you get two records in the same nano-second as making the call System.nanoTime() takes over 100 ns. It is more likely your clock doesn't have nano second accuracy.
我严重怀疑您在同一纳秒内获得两条记录,因为调用 System.nanoTime() 需要超过 100 ns。您的时钟很可能没有纳秒精度。
However, if you restart your server, you can get repeating nanoTime().
但是,如果您重新启动服务器,则可能会重复 nanoTime()。
One way around this is to use
解决此问题的一种方法是使用
AtomicLong counter = new AtomicLong(System.currentTimeMillis()*1000);
long id = counter.incrementAndGet();
// something like ctz9yamgu8
String id = Long.toString(counter.incrementAndGet(), 36);
This will start a counter when the application restarts and they will not be overlap between restarts unless you sustain over one million ids per second. (Over the life of the instance)
这将在应用程序重新启动时启动一个计数器,并且除非您每秒维持超过一百万个 id,否则它们不会在重新启动之间重叠。(在实例的生命周期内)
Note: this only works for on a per instance basis. Multiple servers need to use a different approach.
注意:这仅适用于每个实例。多个服务器需要使用不同的方法。
回答by Basil Bourque
There seems to be some confusion on this page about the nature of UUID.
此页面上似乎对 UUID 的性质有些困惑。
Study the Wikipedia page. You will see there are different versions of UUID.
You asked:
你问:
Does UUID solves the above problem ?
UUID 是否解决了上述问题?
Yes, UUID values do solve your problem.
是的,UUID 值确实可以解决您的问题。
A point in space and time
空间和时间中的一个点
The original Version 1represents a point in space and time, never to be repeated.
最初的版本 1代表了空间和时间的一个点,永远不会重复。
Version 1 does this by using the MAC addressof the machine on which it is generated (a point in space). To this it combines the current moment. Add in an arbitrary number that increments when a change in the computer clock is noticed. The clock is not as much of an issue now that computers have built-in batteries and network connections to time servers. By combining these, there is no practical chance of collisions.
版本 1 通过使用生成它的机器的MAC 地址(空间中的一个点)来实现这一点。为此,它结合了当前时刻。添加一个任意数字,当注意到计算机时钟发生变化时该数字会增加。由于计算机具有内置电池和与时间服务器的网络连接,因此时钟不再是一个问题。通过组合这些,实际上不会发生碰撞。
Because of concerns over the security and privacy issues involved in tracking and divulging the MAC address and moment, some people may not want to use this version. For example, Javaomits generating Version 1 from its UUID
class.
由于担心跟踪和泄露 MAC 地址和时刻所涉及的安全和隐私问题,有些人可能不想使用此版本。例如,Java省略了从它的UUID
类生成版本 1 。
FYI, the more powerful database servers such as Postgres can generate UUID values including Version 1. You may choose to generate your UUIDs on the database server rather than in your app.
仅供参考,更强大的数据库服务器(例如 Postgres)可以生成包括版本 1 在内的 UUID 值。您可以选择在数据库服务器上而不是在您的应用程序中生成您的 UUID。
Random
随机的
One commonly used version is Version 4, in which 122 of the 128 bitsare generated randomly. If a cryptographically-strong random generatoris used, this is quite effective. This version has a much higher chance of collisions than in Version 1. But for most practical scenarios, the random-based UUID is entirely reliable.
一种常用的版本是Version 4,其中128 位中的122位是随机生成的。如果使用密码强的随机生成器,这是非常有效的。与版本 1 相比,此版本发生冲突的几率要高得多。但对于大多数实际场景,基于随机的 UUID 是完全可靠的。