java


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.

Related Links

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

Categories

HOME
ms-access
date
coq
google-play
onedrive
paw-app
malloc
wamp
flyway4
bs4
communication
vifm
spring-xd
rascal
nstableview
gz
size
leiningen
jsprit
openrefine
apache-metamodel
ab-testing
normalizr
nhibernate-envers
google-cloud-speech
riot.js
su
web-api-testing
replaceall
opentype
ejabberd-module
kendo-datasource
newline
r-raster
galsim
google-sites-2016
vxworks
dartium
streamreader
tasker
awt
ws-security
eclipse-scout
avro4s
dynamic-reports
acoustics
node-sass
angular-resource
outlook-2013
rainbowtable
hendrix
domain-model
powercli
ifs
sonarlint-vs
pycaffe
static-ip-address
spring-android
gridpane
ubuntu-10.04
make-install
rvest
ipconfig
itextpdf
wapiti
unity-networking
vhd
execute
marching-cubes
wireshark-dissector
cakephp-3.1
apache-commons-fileupload
c++03
kineticjs
csquery
modalpopup
jscript.net
codeigniter-routing
socketexception
java-metro-framework
typeof
p4java
seaside
ember-charts
farseer
quantlib-swig
sitemesh
dbconnection
gridcontrol
qt-faststart
jquery-mobile-dialog
drools-planner
delphi-6
clipper
tridion-worldserver
ocx
yetanotherforum
recent-documents
krl
actionview
android-sdk-2.1
sproutcore-2
multi-tier
mtj
avatar
xetex
privilege
document-conversion
uiq3

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App