This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate gives a Constraint Violation Exception..

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using Hibernate to save a POJO to MySQL database�
Table-Definition:


POJO:


Mapping file:

Hibernate Config file:

The Committer source code:

The Tester:

Problem/Questions:
1.When I run the Tester, I get ConstraintViolationException uplicate Key. Out of the 10 saves(1 per thread) Hibernate manages to saves a random number of rows and then it gives this exception.

2.I am depending on Hibernate�s id generation strategy for generating primary keys(viz. increment), and I am getting a constraint violation.
(Assuming a problem with the ID generation strategy)I thought the problem could be with runtime data not being written to database(perhaps, the ID generator couldnot get the latest Max value of the column) so, I added a session.flush() call, but to no avail...
Finally, I managed to run this code, by removing the generator tag altogether and by modifying my DEMO schema. I added an auto_increment to the primary key column.

Is this a known issue or is there something that I overlooked?
3. How does the session.flush() work? If it synchronizes the database with the runtime persistent data, why do I get this exception(assuming my supposition in 2 above is correct)?

Please enlighten�
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a thread safety issue, with the same primary keys generated in each of the threads. This is why a generator on the database side (MySQL autoincrement) removes the exception.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When does "ID-generation" actually happen? I am under the impression that every time a session is opened and we try to do a session.save() a new ID would be generated. So, if multiple threads are invoking same method that opens a hibernate session and tries to do a session.save() they should have gotten different IDs.

Also, the method that actually commits data is "static", aren't static methods thread safe?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is a thread safety issue, with the same primary keys generated in each of the threads. This is why a generator on the database side (MySQL autoincrement) removes the exception.

Though it does add the downside of introducing a database dependency. Better to use assigned, and implement your own key generation strategy (such as Hi-Lo keys)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, the method that actually commits data is "static", aren't static methods thread safe?

No. They can be, but only if they are written as such. There is nothing inherent in static methods that makes them threadsafe.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though it does add the downside of introducing a database dependency. Better to use assigned, and implement your own key generation strategy (such as Hi-Lo keys)


I second that and that is why i didnt like the idea of using auto increment.Auto increment was sort of a temporary fix.

Ok, i was wrong in assuming that static methods are thread safe. Will making the method static synchronize help?
 
arulk pillai
Author
Ranch Hand
Posts: 3393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you are calling within a single JVM (i.e process). Not if you run in a clustered server environment.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i am running it on a single JVM and declaring the method as static synchronized did not give errors.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic