我在编写真实的代码之前创建了一个测试代码,以确保它能正常工作。这段代码所做的就是在彼此内部启动新的事务,并尝试更新两个实体。很简单对我来说,在它离开transaction 3()方法后,它应该回滚transaction 2 Entity update,但它没有发生。
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;
}
我开始我的测试代码调用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();
}
}
这是主类:
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();
}
}
我该怎么弥补?
1条答案
按热度按时间ny6fqffe1#
我不知道为什么,但是当我将transaction3()移到另一个EJB类时,它工作了。我所做的就是那样。