写了个序列,并且在触发器中指定自动生成id,但是遇到了生成主键不连续的问题。表现在隔一段时间运行时,生成的主键不连续。
创建序列的语句如下:
create sequence PROJECT_ID_GENERATE
increment by 1
start with 0
nomaxvalue
nocycle;
从论坛中得知,遇到上述问题有如下几种情况:
1.在这段时间内,别人或者别的程序使用这个序列了,造成你使用时不连续:
但是我是在自己的机器上来做的,没有别的程序使用,更没有别人使用。这种情况就排除了。
2.做过ROLLBACK,但是序列值不会再变小。
除非Oracle自动给我做了ROLLBACK,否则这种情况也排除。
3.创建的序列没有设置nocache:
根据oracle的文档:
NOCACHE
specifies that values of the sequence are not preallocated.
If you omit both the CACHE parameter and the NOCACHE option, Oracle
caches 20 sequence numbers by default. However, if you are using
Oracle with the Parallel Server option in parallel mode and you
specify the ORDER option, sequence values are never cached,
regardless of whether you specify the CACHE parameter or the NOCACHE option.
就是这一条,于是赶紧修改序列:
alter sequence PROJECT_ID_GENERATE
nocache;