I have a Spring and hibernate application (both latest version) and I have 2 beans as mentioned below
@Component
public class Bean1{
@Autowired
Bean2 bean2;
@Transactional(propagation = Propagation.REQUIRED)
public void foo()
{
bean2.bar();
}
@Component
public class Bean2{
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void bar()
{
try{
// Do something which throws exception
}
catch (Exception e) {
log & eat the exception here only.
But inspite of this the outer transaciton gets rolled back
}
}
The issue is that when bean2.bar
causes any exception (e.g. foreign Key ConstraintViolationException
) then it rolls back the outer transaction as well saying " Transaction rolled back because it has been marked as rollback-only","moreInfo":""}"
On seeing hibernate logs I found only one line for "new transaction"
D| o.s.o.h.HibernateTransactionManager- Creating new transaction with name ... PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
which means no new transaction is getting created for the inner bean2.bar();
I am not able to find out what's wrong here? Any help is greatly appreciated.