-->
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.  [ 4 posts ] 
Author Message
 Post subject: Two processes inserting a row
PostPosted: Wed Mar 10, 2010 6:46 pm 
Newbie

Joined: Wed Mar 10, 2010 6:34 pm
Posts: 2
Hi,

I have the following requirement:

1. Select a particular row from a table..
2. If the row is not found, insert a new one.

Now if two processes try to do the same concurrently, one of them will get a unique constraint (primary key) violation on step 2. Both processes will try to insert the same exact row, so there is no data correctness issue.

This looks like it might be a pretty standard problem. Is there there a "standard" solution for this in hibernate.

Thanks,
-Gautam


Top
 Profile  
 
 Post subject: Re: Two processes inserting a row
PostPosted: Thu Mar 11, 2010 3:50 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
For me the 'standard solution' for this standard problem is to
properly catch and handle the unique constraint violation exception in your implementation.

1. lookup a row from a table with a determinate key.
2. If the row is not found, try to insert a new one.
3. If constraint violation exception raises, rollback, begin a new transaction and re-select from the table with the key.
Now you should find the record which in meantime was inserted by another process.


Top
 Profile  
 
 Post subject: Re: Two processes inserting a row
PostPosted: Thu Mar 11, 2010 12:38 pm 
Newbie

Joined: Wed Mar 10, 2010 6:34 pm
Posts: 2
Yes, of course. I guess my question should have been, is there a standard db-independent 'unique constraint violation' exception that I can use?

Thanks,
-Gautam


Top
 Profile  
 
 Post subject: Re: Two processes inserting a row
PostPosted: Fri Mar 12, 2010 3:35 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
is there a standard db-independent 'unique constraint violation' exception that I can use?


I fear not.
Per definition a JDBC-Driver has to throw only exceptions of type java.sql.SQLException.
There's no common definition 'how' unique constraint violations have to be wrapped into a java.sql.SQLException.
This is vendor-specific.

Quote:
SQLException.getErrorCode() Retrieves the vendor-specific exception code for this SQLException object.


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

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.