DessDess
DessDess

Reputation: 787

Rollback with catched RuntimeException

I clearly need some help about transaction roll back. I'm working on a Spring/JPA/Hibernate application.

For me, RuntimeException even if they are catched, are rolling back the transaction. I deduce this with some tests (that I can't put here unfortunatly as I don't have them anymore) and readings.

But I'm experiencing another behaviour with the following code :

public class Service implements IService {

  @Transactional
  public void test()
  {
    // ...
    try {
      throw new RuntimeException();
    } catch (RuntimeException re) {
    }
    foo.setBar(barValue);
    this.fooDao.save(foo);
  }
}

After executing this from the controller, the change on barparameter is present in my database which mean that the transaction has not been rollbacked.

Now the question

Does a catched runtimeException lead to a rollback or am I wrong ?

Upvotes: 0

Views: 3408

Answers (1)

Pradeep Pati
Pradeep Pati

Reputation: 5919

If you catch an unchecked exception e.g. RuntimeException, the transaction will not be rolled back.

By default, if your method exits because of an unchecked exception, then the transaction will be rolled back. Checked exceptions by default will not trigger rollback.

Upvotes: 1

Related Questions