-
1. Re: EJB 3 Session Bean does not rolling back transaction
jaikiran Sep 26, 2014 7:53 AM (in response to arnabban5)Please post the relevant code including the way you access and invoke on the bean.
-
2. Re: EJB 3 Session Bean does not rolling back transaction
arnabban5 Sep 27, 2014 2:42 AM (in response to jaikiran)@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SessionBeanFacade implements SessionBeanFacadeRemote {
@PersistenceContext(unitName="EjbComponentPU") EntityManager em;
@EJB
InnerBeanLocal innerBean;
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void insertRows() {
System.out.println("Start insertRows");
FamilyCar familyCar = new FamilyCar();
familyCar.setIsAirConditioned(true);
familyCar.setMake("Maker1");
Aeroplane aeroplane = new Aeroplane();
aeroplane.setModel("Air Asia");
aeroplane.setMake("Air Asia");
aeroplane.setSeatNum("500");
em.persist(familyCar);
em.persist(aeroplane);
System.out.println("End insertRows");
innerBean.insertRows();
}
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class InnerBean implements InnerBeanLocal {
@PersistenceContext(unitName="EjbComponentPU")
EntityManager em;
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void insertRows() {
System.out.println("Start insertRows in innerBean");
Aeroplane aeroplane = new Aeroplane();
aeroplane.setModel("Lufthansa");
aeroplane.setMake("Lufthansa");
aeroplane.setSeatNum("500");
aeroplane.setSize("Big");
em.persist(aeroplane);
throw new RuntimeException("DEliberate RuntimeException from innerbean.......");
}
}
From this code I was expecting the "Lfthansa aeroplane" to be rolled back only, and other two rows from OuterBean to be persisted. But NOTHING gets persisted.
-
3. Re: EJB 3 Session Bean does not rolling back transaction
jaikiran Sep 27, 2014 4:18 AM (in response to arnabban5)Arnab Banerjee wrote:
...
From this code I was expecting the "Lfthansa aeroplane" to be rolled back only, and other two rows from OuterBean to be persisted. But NOTHING gets persisted.
That's because your outer bean which is calling the method on the inner bean, doesn't handle/catch the exception that's thrown by the inner bean. Which effectively ends up as a RuntimeException being thrown by the outer bean too. So both inner and outer beans are throwing the exception which results in both the transactions rolling back and thus nothing being persisted.
-
4. Re: EJB 3 Session Bean does not rolling back transaction
arnabban5 Sep 28, 2014 1:56 AM (in response to jaikiran)Thanks jaikiran