2 Replies Latest reply on Nov 17, 2014 8:54 AM by Juan Martin Runge

    Wildfly CMT Transactions

    Juan Martin Runge Newbie

      Hi,

      Im having a hard time trying something quite simple to work (code simplified for the sake of the example):

       

      @LocalBean

      @Stateless

      public class DummyService {

           @PersistenceContext

           private EntityManager em;

       

           public void persistBean(Object bean) throws PersistenceException {

                try {

                     em.persist(bean);

                     doSomethingElse();

                } catch(PersistenceException ex) {

                     throw ex;

                } catch (Exception ex) {

                     throw new PersistenceException("Failed", ex);

                }

           }

       

           private void doSomethingElse() throws PersistenceException {

                Object b = em.find(Dummy.class, 1);

                throw new PersistenceException("Please rollback!")

           }

      }

       

      @ApplicationException(rollback=true)

      public class PersistenceException extends Exception {

       

       

          public PersistenceException(String message) {

              super(message);

          }

       

       

          public PersistenceException(String message, Throwable cause) {

              super(message, cause);

          }

         

      }

       

      Another stateless SessionBean calls persistenceBean.persistObject(a).  I would expect that a wont get persisted due the exception thrown from doSomethingElse method (in fact, I expected transaction to be rolled back).  But this is not happening!  Object a gets persisted and I get the exception.  What should I do to make a rollback or to force EntityManager to rollback the transaction?

      I tried setting TransactionAttribute to REQUIRE_NEW for persistObject method and to MANDATORY for doSomethingElse method, but this didnt worked either.  I tried to use "setRollbackOnly" but didnt worked either. 

      Im lost.  Please help!

       

      Thanks in advance,

       

      Juan Martin