4 Replies Latest reply on Sep 28, 2014 1:56 AM by arnabban5

    EJB 3 Session Bean does not rolling back transaction

    arnabban5

      Hi,

       

      I am using JBoss EAP 6.3 and HSQL DB. I was trying to test the transaction rollback for CMT bean.

       

      From my transaction aware method (Annotated with REQUIRED attribute), I persisted one entity bean then thrown one RuntimeException, expeting the transaction to rollback by the server resulting in no DB insertion. But to my surprise I found the data getting inserted in DB.

       

      Please help. I tried with custom checked exception as well(Marking with @ApplicationException(rollback=true)).

      Thanks,

      -Arnab

        • 1. Re: EJB 3 Session Bean does not rolling back transaction
          jaikiran

          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

            @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

              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

                Thanks jaikiran