[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Efficient concurrent insert and unique constraint

dcausse do-not-reply at jboss.com
Fri May 16 06:07:40 EDT 2008


Hi,

Imagine a table Foo with :
ID   PRIMARY
BAR UNIQUE

For concurrent insert on this table at SQL/JDBC level I do :


  | 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 instanceof ConstraintExceptionOnBar)) {
  |             // If other session has committed my bar
  |             // then I will have to use this one and continue
  |             foo = foodao.findByBar(bar);
  |        } else {
  |             thow new MyException(sqle);
  |        }
  |    }
  | }
  | 

With EJB3 entities I can't do this...
Even if I catch EntityExistsException transaction is invalidated, I've read that transaction are all invalidated by hibernate if an exception occur. IMHO it is safe and efficient here to rely on DB locking mechanism and error handling, how can I be as efficient here with EJB3 entities? 
I don't want to restart all my transaction...

Any suggestions?

Thank you.


David.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4151328#4151328

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4151328



More information about the jboss-user mailing list