2 Replies Latest reply on Nov 17, 2014 8:54 AM by jmrunge

    Wildfly CMT Transactions

    jmrunge

      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

        • 1. Re: Wildfly CMT Transactions
          smarlow

          Sounds like your persistence unit is not configured for JTA or the Resource was delisted from the transaction.  Have you enabled TRACE logging for the transaction manager yet?  Steps are described here.  You probably want TRACE logging for com.arjuna and org.jboss.as.jpa.

           

          Also make sure that your persistence.xml does not have transaction-type=RESOURCE_LOCAL.  You should also have a jta-data-source.

           

          Scott

          • 2. Re: Wildfly CMT Transactions
            jmrunge

            Thanks Scott!  I havent event noticed the JTA checkbox when defining DS on Wildfly.  My persistence unit had transaction-type=JTA, but JTA was not enabled on the DS.  Now it works as expected.