DB2 BIGINT id 生成器
我将以下类加载到 DB2 9.7.4 Express-C 中,用于生成行的 id
public class Int64UUID {
public static final long dx = 30*386*12*30*24*3600*1000; // starting at 2000 year
public static long lastUUID = System.currentTimeMillis() - dx;
public static synchronized long random(){
long uuid = System.currentTimeMillis() - dx;
while(uuid == lastUUID)
uuid = System.currentTimeMillis() - dx;
lastUUID = uuid;
return uuid;
}
public static void main(String[] args) {
System.out.println(Int64UUID.random());
}
}
,并使用以下函数使用它。
CREATE FUNCTION "MYSCHEMA"."INT64_GUID" ( )
RETURNS BIGINT
SPECIFIC "SQL110520165927000"
LANGUAGE JAVA
PARAMETER STYLE JAVA
EXTERNAL NAME 'Int64UUID.random'
NOT DETERMINISTIC
NO EXTERNAL ACTION
NO SQL
CALLED ON NULL INPUT
DISALLOW PARALLEL;
由于同步,使用这些函数生成的 id 在 db2 会话中是否是唯一的,这些是 GENERATED ALWAYS 的更好替代方案吗?作为身份,我知道这些身份将在未来 60 年内过期,但 60 年是很长的一段时间
I have the following class loaded into DB2 9.7.4 Express-C for generating ids for rows
public class Int64UUID {
public static final long dx = 30*386*12*30*24*3600*1000; // starting at 2000 year
public static long lastUUID = System.currentTimeMillis() - dx;
public static synchronized long random(){
long uuid = System.currentTimeMillis() - dx;
while(uuid == lastUUID)
uuid = System.currentTimeMillis() - dx;
lastUUID = uuid;
return uuid;
}
public static void main(String[] args) {
System.out.println(Int64UUID.random());
}
}
and the following function for using it
CREATE FUNCTION "MYSCHEMA"."INT64_GUID" ( )
RETURNS BIGINT
SPECIFIC "SQL110520165927000"
LANGUAGE JAVA
PARAMETER STYLE JAVA
EXTERNAL NAME 'Int64UUID.random'
NOT DETERMINISTIC
NO EXTERNAL ACTION
NO SQL
CALLED ON NULL INPUT
DISALLOW PARALLEL;
Will ids generated using these function be unique across db2 sessions due to synchronization, and is these a better alternative to GENERATED ALWAYS AS IDENTITY, I know that those id's will expire in next 60 years but 60 years is long period
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些 ID 可能不是唯一的。如果不同 JVM 中有两个会话同时生成 ID,则毫秒分辨率不足以确保您获得唯一的 ID。
考虑使用内置的
UUID
。但是,这将产生 128 位 UUID。但它即使跨会话、甚至在不同的 JVM 中也能确保唯一性。还有另一个堆栈溢出讨论,它解决了如何
生成 long 类型的 UUID 。
These IDs will likely not be unique. If you have two sessions in different JVMs generating IDs at the same time millisecond resolution is not large enough to ensure you will get unique IDs.
Consider using the built in
UUID
. However, this will produce a 128 bit UUID. But it ensures uniqueness even across sessions, even in different JVMs.There is another stack overflow discussion which addresses how to
generate UUID of long type .
我对表
IBM DB2 - GENERATE_UNIQUE
这里有一篇关于developerworks的文章,名为生成通用唯一标识符 (UUID)
这可能有帮助吗?
I use GENERATE_UNIQUE with a trigger ON INSERT for the table
IBM DB2 - GENERATE_UNIQUE
Here's an article on developerworks called Generating universally unique identifiers (UUID)
which may help?
您的 dx 值看起来有点随机。它太大,无法放入
int
中,并且会溢出,但它看起来也不正确。您有 30 *(30 年)* 386(不确定这是否应该是 356)* 12(每年的月份)* 30(典型月份中的天数)You
dx
value looks a bit random. It is too large to fit in anint
and will over flow but it also doesn't appear correct. You have 30 * (30 years) * 386 (not sure if this is supposed to be 356) * 12 (months per year) * 30 (days in a typical month)