質問

we have deployed Spring 3 + Hibernate application on 2 load balancing servers.

After 50 insertions into the Database, hibernate is not calling the DB sequence nextval, it continuously incrementing the count. And due to this, the sequence values from both the servers are overlapping and I am getting "Unique Constraint Violation" exception.

E.g. Say, first time the sequence values from both the servers are 100 and 150 respectively. When the sequence of first instance reaches 149, it is supposed to call seq.nextVal. But since it is not doing so and continuously incrementing the seq value to 150 which is used by other instance and it is failing. default allocationSize: 50

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOMER_ID_SEQ")
@SequenceGenerator(name = "CUSTOMER_ID_SEQ", sequenceName = "CUSTOMER_SEQ")
@Column(name = "CUST_ID", unique = true, nullable = false)
public long getCustId() {
return custId;
}

Thanks in advance.

役に立ちましたか?

解決

If you're using load-balancers, but the DB is shared between the instances, you cannot cache the sequences' values.

You have to ask for the next sequence value each time you insert into DB. And each insert has to be inside a transaction. Hibernate, (by default) has allocationSize = 50, so to fix your problem, you have to explicitly set it to 1.

 @SequenceGenerator(allocationSize=1 ...)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top