The two or more user trying to insert table getting unique constraint violation exception in Hibernate. How to handle can anyone help me.Thanks in advance.
-
2You should accept one of the answers to your previous questions, otherwise people stop answering your questions. – Stefan Steinegger May 09 '12 at 06:37
-
check your ID generator stratergy for the [refer this question for more on ID generation][1] [1]: http://stackoverflow.com/questions/495536/hibernate-id-generator – Anantha Sharma May 09 '12 at 06:59
1 Answers
This error always appears if you want to insert a row with a value in a column having a unique index or constraint, and the inserted value for this column already exists in an other row. This might be an issue with concurrent access in multithread environments.
Depending of the source of the problem there are some strategies to solve it:
- Use a generator (sequence or similar) for generating a unique id. Hibernate helps you with the <generator>
tag inside the <id>
tag.
- Use the <version>
tag to avoid concurrent modification of the same entity
- write a method which generates unique values
- synchronise parts of the code with the Java synchronized
statement
- use a stateless session and after exception retry to insert with a new value
- correct logical errors in your code
- and some more...

- 5,223
- 1
- 21
- 38