我正在努力使用 WAS8.5 使用简单的 Rest Service + EJB 将实体插入到我的 DB2 数据库中。选择工作正常,因为不需要交易。实体已正确注释。(成功的选择至少证明了这一点)
我总是得到以下异常:
javax.persistence.TransactionRequiredException: No active transaction for PuId=project.ear#included.war#persistence-unit-name
[29.08.13 15:14:59:714 CEST] 00000071 SystemErr R at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:221)
[29.08.13 15:14:59:714 CEST] 00000071 SystemErr R at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:179)
[29.08.13 15:14:59:714 CEST] 00000071 SystemErr R at com.ibm.ws.jpa.management.JPAEntityManager.persist(JPAEntityManager.java:143)
[29.08.13 15:14:59:714 CEST] 00000071 SystemErr R at com.TestFacade.testEMInsert(TestFacade.java:72)
我的 persistence.xml(放在 META-INF 中):
<?xml version="1.0" encoding="UTF-8"?>
<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="persistence-unit-name"
transaction-type="JTA">
<!-- <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> -->
<!-- <provider>com.ibm.websphere.persistence.PersistenceProviderImpl</provider> -->
<jta-data-source>jdbc/test-services-ds</jta-data-source>
<class>com.EntityTest</class>
<properties>
<!--<property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>-->
<property name="openjpa.Log" value="SQL=TRACE"/>
<!-- PrintParameters will show query parameters, remove when development has been finished -->
<!-- <property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true"/> -->
<property name="openjpa.jdbc.Schema" value="LKWSQL"/>
<property name="openjpa.TransactionMode" value="managed"/>
<property name="openjpa.ConnectionFactoryMode" value="managed"/>
<property name="openjpa.jdbc.DBDictionary" value="db2"/>
</properties>
</persistence-unit>
</persistence>
将读取persistence.xml,所以我认为耳朵的包装似乎没问题。我知道在 JavaEE6 中一场战争就足够了,但它已经是一个现有的项目设置。我们之前的团队切换到用户管理的交易,但我希望有人可以帮助我指出错误。
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED) // default, but was worth a try
@Path("servicepath")
public class TestFacade implements ITestFacade {
@Inject
private SearchService searchService;
@PersistenceContext(unitName="persistence-unit-name")
private EntityManager em;
搜索服务引起了第一个麻烦。使用@EJB,它始终为空。CDI 的@Inject 有效。也许这是一个线索?
@GET
@Path("testInsert/")
@Produces({ MediaType.TEXT_PLAIN })
public String testEMInsert() {
try{
System.out.println("testing Insert: " + em.isOpen()); // open is true
EntityTest e = new EntityTest("name");
em.persist(e);
} catch (Exception e){
e.printStackTrace();
return e.getMessage();
}
return "OK";
}
我希望有人知道我可以检查什么。不得不更改外观+服务名称。:-/ 问候米