I clearly need some help about transaction rollback. I'm working on a Spring/JPA/Hibernate application.
For me, RuntimeException even if they are caught, are rolling back the transaction. I deduce this with some tests (that I can't put here unfortunately 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 bar parameter is present in my database which means that the transaction has not been rollbacked.
Now the question
Does a caught runtimeException lead to a rollback or am I wrong ?
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.