I created a test code before make the real code just to make sure it would work. All this code does is start new transactions within each other and tries to update two entities. It's simple. For me, after it leaves transaction3() method it should roll back transaction2Entity update, but it doesn't happen.

persistence.xml

<persistence version="2.0"
   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="sac">      
      <jta-data-source>java:jboss/datasources/sacDS</jta-data-source>
      <class>org.hibernate.envers.DefaultRevisionEntity</class>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
         <property name="hibernate.show_sql" value="false"/>
         <property name="hibernate.format_sql" value="true"/>         
         <property name="hibernate.hbm2ddl.auto" value="validate"/>         
      </properties>     
   </persistence-unit>
</persistence>

EnvioException

@javax.ejb.ApplicationException(rollback=true)
public class EnvioException extends javax.ws.rs.WebApplicationException {

    private static final long serialVersionUID = -990622645054458959L;

}

I start my test code calling the REST API "/transactionService/transaction":

API REST

@Path("/transactionService")
public class TransactionService {
    @EJB
    TestTransaction testTransaction;

    @Path("/transaction")
    @GET
    @SemAutenticacao
    public void transaction(@Context HttpHeaders headers) throws IOException, SQLException {
        testTransaction.transaction1();
    }
}

This is the main class:

TestTransaction

@Stateless
public class TestTransaction {

    @Inject
    protected EntityManager em;

    public void transaction1() throws IOException, SQLException {
        transaction2();
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void transaction2() throws IOException, SQLException {
        TransactionEntity transactionEntity = em.find(TransactionEntity.class, 1L);
        transactionEntity.setNome("Joe");
        
        try {
            transaction3();
        } catch(EnvioException e) {
            System.out.println("Exception Handled here!");
        }
    }
        
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void transaction3() throws EnvioException {
        Transaction2Entity transaction2Entity = em.find(Transaction2Entity.class, 2L);
        transaction2Entity.setNome("Joe");
        
        throw new EnvioException();
    }
}

How could I fix it ?

1

There are 1 answers

0
Renato On

I don't know why, but when I moved transaction3() to another EJB class, it worked. All I did was just that.