java - Oracle 序列事务性

标签 java sql oracle hibernate sequence-sql

我需要针对特定​​的业务场景在实体(不是 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/

相关文章:

java - 如何使用 OnClick 事件的命令参数动态添加按钮

java - java.lang.RuntimeException 的未知原因

java - Switch 声明调查

mysql - 是否在对 mysql 表中的列建立索引之前插入索引的值

sql - NOT IN运算符问题Oracle

oracle - ORACLE PL/SQL 中有计算多边形面积的函数吗?

java - 使用 JUnit、Spring 4 和 Vaadin 对类进行单元测试时获取 NPE

MySQL:表达两个具有公共(public)基表外键的表的完全联接

mysql - 如何逐个获取数据库(oracle)表意味着首先它显示第一行,然后在提交时它将显示下一行,依此类推

oracle - 按一定数量递增 Oracle 序列