I have a one-to-one mapping between two objects, Meeting and Minutes.
Meeting “A” is created first, then the Minutes can optionally be created after the meeting takes place.
User 1 and User 2 try to create the minutes at the same time for Meeting A, and a primary key constraint exception arises at the database level.
I would like to be able to gracefully handle this event. I can catch the exception when this happens and deliver to the user, but I am wondering if there is an alternative option provided by hibernate that I am missing?
I have tried doing this code in java:
synchronized ( meeting ) {
if ( meeting.getMinutes == null ) {
// create new minutes
}
}
but because I am using a transaction per request pattern, the commit doesn’t come until later, after synchronized section ends, and so 2 concurrent threads still try to both create the minutes. is there a way these 2 sessions / transactions can communicate better?