views:

344

answers:

2

HI All,

I am trying to save name, address, email information using Hibernate...sometimes there is an exception thrown by Hibernate, this causes JBoss to rollback the full transaction! I do not want this to happen, if there is a Hibernate exception while saving email... then I just want the email bit to rollback not the entire, name, address, email to rollback -which is what JBoss does. I do have a try-catch block around the save operation and even though the exception is caught, Jboss still continues with the rollback.

Does anybody have any suggestions as to how I can implement this?

Many thanks.

A: 

What you need is a nested transaction, unfortunately hibernate doesn't support nesting transactions, so I'm afraid your out of luck. Your only real choice is to perform the update in two separate transactions.

YourTransactionalService service = ;

service.updateNameAndAddress(details);

try{
    service.updateEmail(details);
}catch( HibernateException e){
    // could just ignore it?
}

You might what to look at why Hibernate is throwing an exception and maybe validate the data upfront to prevent it in the first case, this may address the actual underlying problem rather than my solution here which just side steps it.

Gareth Davis
+1  A: 

The above is a good suggestion, but I have a bit more to add:

If you are inside JBoss, are you using Hibernate directly, or are you using JPA (EJB3.0)? If you are using EJB, you can use nested transactions using the @TransactionAttribute annotation on your method calls into a Stateless Session Bean.

If you are using Hibernate directly, you should really consider using the abstraction layer (JPA) that JBoss provides (assuming you are running JBoss 4+)

Chaos