Ensuring unique serial numbers in a Hibernate session
I am writing a system that holds a hibernate-managed entity called Voucher that has a field named serialNumber, which holds a unique number for the only-existing valid copy of the voucher instance. There may be old, invalid copies in the database table as well, which means that the database field may not be declared unique. The operation that saves a new valid voucher instance (that will need a new serial number) is, first of all, synchronized on an appropriate entity. Thereafter the whole procedure is encapsulated in a transaction, the new value is fetched by the JPQL SELECT MAX(serialNumber) + 1 FROM Voucher the field gets the result from the query, the instance is thereafter saved, the session is flushed, the transaction is committed and the code finally leaves the synchronized block. In spite of all this, the database sometimes (if seldom) ends up with Vouchers with duplicate serial numbers. My question is: Considering that I am rather confident in the synchronization and transaction handling, is there anything more or less obvious that I should know about hibernate that I have missed, or should I go back to yet another debugging session, trying to find anything else causing the problem? The service running the save process is a web application running on tomcat6 and is managed by Spring's HttpRequestHandlerServlet. The db connections are pooled by C3P0, running a very much default-based configuration. I'd appreciate any suggestion Thanks
You can use a MultipleHiLoPerTableGenerator: it generate #Id outside current transaction.
You do not need to debug to find the cause. In a multi-threaded environment it is likely to happen. You are selecting max from your table. So suppose that TX1 reads the max value which is a and inserts a row with serial number a+1; at this stage if any TX2 reads DB, the max value is still a as TX1 has not committed its data. So TX2 may insert a row with serial number of a+1 as well. To avoid this issue you might decide to change Isolation Level of your database or change the way you are getting serial numbers (it entirely depends on circumstances of your project). But generally I do not recommend changing Isolation Levels as it is too much effort for such an issue.
javax.websocket client android app simple example
I need to use java 8 filter to filter my list of custom objects in efficient manner
Google Speech API Java Client does not work on my computer
My program gives me error ArrayIndexOutOfBoundsException [duplicate]
http component can't connect to proxy
Clone a Set including real type
how can i display a message dialog box in java
JTextArea admin password control
Playing Youtube videos with the Youtube API and an ExpandableListView - ANDROID STUDIO
maven jar plugin with specific filename not work
Need help, trying to read a file and fill an array of objects with their parameters in java
BufferedReader and BufferedWriter with Socket
Is String a immutable class in JAVA, So how to use this feature is multi-Thread programing?
Loading drawable into Fragment
Print with Lambda foreach in the same line with whitespace
Accessing static classes in another java file