【发布时间】:2020-08-12 02:57:00
【问题描述】:
我正在使用带有 Hibernate JPA.Oracle 版本 - 12c 的 SpringBoot 应用程序,具有 3 个节点的 RAC 设置。我有四个应用服务器。 对于下表:
@EntityListeners(AuditingEntityListener.class)
@Data
@Entity
@Table(name="order_payment_collection")
public class OrderPaymentCollection {
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
Long id;
....
}
我在哪里使用 AUTO 生成策略,因此 hibernate 在 DB 中创建了一个序列:
CREATE SEQUENCE "test"."HIBERNATE_SEQUENCE" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1798892 CACHE 20 NOORDER NOCYCLE NOPARTITION ;
当两个不同的进程(在不同的节点上运行)发生并发插入时,我收到以下错误:
could not execute statement; SQL [n/a]; constraint [*.SYS_C005080]; nested exception is
org.hibernate.exception.ConstraintViolationException: could not execute statement
这是主键/唯一索引 - 此表的列 ID。
是否需要在使用 Hibernate 时为每个节点/进程使用单独的序列?
我有一个解决这个问题的计划:
@GenericGenerator(name = "seq", strategy = "com.test.utils.KeyGenerator")
@GeneratedValue(generator = "seq")
@Id
Long id;
在 KeyGenerator 类中,我正在实现 IdentifierGenerator 接口,从自定义 DB 序列中获取 nextVal。
CREATE SEQUENCE test."PE_TABLES_SEQ" MINVALUE 10000 MAXVALUE 10000000000000 INCREMENT BY
1 START WITH 1798893 CACHE 1000 NOORDER NOCYCLE NOPARTITION ;
因此希望获得专家意见,如果我遗漏任何内容或有人遇到此问题。我解决该问题的解决方案是否有效?
【问题讨论】:
标签: java hibernate jdbc spring-data-jpa ojdbc