-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Handling duplicate key in concurrent context
PostPosted: Tue Mar 06, 2007 3:30 pm 
Newbie

Joined: Mon Mar 05, 2007 5:12 pm
Posts: 3
Hi,

I'm trying to figure out how I can solve the following problem. Here is the situation. I have 2 entities: delivery and user. The relationship between these entities is many to one: multiple deliveries for one user. When the application persist a delivery, the associated user is either created or the existing one is referenced via a retrieval since users must be unique.

Everything works fine up to this point. The problem is that when there are multiple concurrent Hibernate sessions, it happens that they try to persist the same user. The cause of the problem is that when each process verify the presence of the user, they don't find it so they try to persist it but when the transactions commit, one or more of them fail with a duplicate key constraint violation because the user has already been persisted by another process.

The problem exists since I use optimistic locking with read commited isolation level to handle concurrency.

The solution I can imagine is when a duplicate key error occurs then I could wait a very short delay and retry the whole operation. During the retry, the existing user would be detected and thus referenced in the delivery.

While this solution should work, I don't really like it because it requires some retry logic development in the persistence layer. I'm wondering if I could specify some instructions in the Hibernate mappings to automate this retry logic?

I use Hibernate 3.1.3 with the Spring's JDBC framework on top of it. The application runs on Linux Redhat enterprise with Java 5.

Thank you for your help in advance.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.