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: Concurrent "find or create" design issue
PostPosted: Tue May 20, 2008 6:49 am 
Newbie

Joined: Tue May 20, 2008 6:25 am
Posts: 1
Hi,

I have a design issue in my code, I need to do a "find or create" an entity in massive concurrent code.
My goal is to rely on DB locking mechanism to handle this and avoid restarting the whole transaction in case of a concurrent issue (I have to handle errors...) :

Imagine a table Foo with :
ID PRIMARY
BAR UNIQUE

For concurrent insert on this table at SQL/JDBC level I do (pseudo code) :

Code:
Foo foo = foodao.findByBar(bar);
if(foo == null) {
   // foobar does not exists or not committed.
   foo = new Foo(bar);
   try {
       // If a second transaction have already inserted bar
       // my insert is locked until other session commit or rollback
       // Of course this insert cannot be delayed
       foodao.save(foo);
       // If the other transaction has rollbacked my insert is successfull
   } catch(SQLException sqle) {
       if(sqle.getErrorCode() == SQL_CODE_UNIQUE) {
            // If other session has committed my bar
            // then I will have to use this one and continue
            // with mysql in this case I can read this value even if I'm in repeatable read transaction.
            foo = foodao.findByBar(bar);
       } else {
            thow new MyException(sqle);
       }
   }
}


How can I do the same with hibernate and preferably JPA?

Will this be possible with the future Session.doWork(Work w) API?
SQLException thrown on the JDBC Connection offered will still invalidate my transaction even if catched?

Thank you.

David.


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.