我需要针对特定的业务场景在实体(不是 PK)上设置一个字段,一个序列中的数字(序列必须是最小值和最大值之间的数字)
我这样定义序列:
CREATE SEQUENCE MySequence
MINVALUE 65536
MAXVALUE 4294967296
START WITH 65536
INCREMENT BY 1
CYCLE
NOCACHE
ORDER;
在 Java 代码中,我从这样的序列中检索数字:
select mySequence.nextval from dual
我的问题是:
如果我在事务中调用此“select mySequence.nextval from dual
”,同时在另一个事务中调用相同的方法(并行请求),则可以肯定顺序不一样?
不可能从第一个事务中读取未提交的值?
因为假设我不会使用序列和一个普通表,我会在其中递增序列,那么如果 trasactinalitY 是默认的“READ COMMITTED”,事务 2 将能够读取相同的值。
最佳答案
答案是否定的。
Oracle保证序列生成的数是不同的。即使发出并行请求,RAC环境或回滚和提交混合。
序列与事务无关。
参见 here the docs :
Use the CREATE SEQUENCE statement to create a sequence, which is a database object from which multiple users may generate unique integers. You can use sequences to automatically generate primary key values.
When a sequence number is generated, the sequence is incremented, independent of the transaction committing or rolling back. If two users concurrently increment the same sequence, then the sequence numbers each user acquires may have gaps, because sequence numbers are being generated by the other user. One user can never acquire the sequence number generated by another user. After a sequence value is generated by one user, that user can continue to access that value regardless of whether the sequence is incremented by another user.
Sequence numbers are generated independently of tables, so the same sequence can be used for one or for multiple tables. It is possible that individual sequence numbers will appear to be skipped, because they were generated and used in a transaction that ultimately rolled back. Additionally, a single user may not realize that other users are drawing from the same sequence.
关于java - Oracle 序列事务性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12104407/