org.apache.openjpa.persistence.PersistenceException: 使用分布式事务时无法设置自动提交模式

org.apache.openjpa.persistence.PersistenceException: Cannot set auto-commit mode when using distributed transactions

我设置了简单的 java ee 项目并使用 jta 事务并使用 OpenJpa 2.4.2 作为 Jpa 提供程序、maven 3.3、eclipse 2020-06、jdk 8U2002、weblogic 12.4.2 和 java ee 6 并得到这个异常:

javax.ejb.EJBException: EJB Exception: : <openjpa-2.4.2-r422266:1777108 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: Cannot set auto-commit mode when using distributed transactions
    at org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:571)
    at org.apache.openjpa.jdbc.meta.MappingTool.record(MappingTool.java:467)
    at org.apache.openjpa.jdbc.kernel.JDBCBrokerFactory.synchronizeMappings(JDBCBrokerFactory.java:160)
    at org.apache.openjpa.jdbc.kernel.JDBCBrokerFactory.synchronizeMappings(JDBCBrokerFactory.java:164)
    at org.apache.openjpa.jdbc.kernel.JDBCBrokerFactory.newBrokerImpl(JDBCBrokerFactory.java:122)

这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
            version="2.0">
            
            <persistence-unit name="batch" transaction-type="JTA" > 
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl </provider>
    <jta-data-source>jdbc/new</jta-data-source>
    <class>com.smartsoft.persistence.Person</class>
    <validation-mode>NONE</validation-mode>
      <properties>
<property name="openjpa.DynamicEnhancementAgent"  value="false"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="unsupported"/>
    <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
      <property name="openjpa.Log" value="DefaultLevel=TRACE"/>
    </properties>
  </persistence-unit>
</persistence>

我有一个简单的实体 class,它有 firstName 和 lastName 以及 age 字段和 setter getter 方法。 和一个无状态 Ejb class,它使用 @PersistenceContext 注释注入 EntityManager 并具有调用 em.persist(Person) 的保存方法然后我将这个 dao 注入一个 servlet 并将一个人 class 传递给它。 这是我的 Weblogic.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">
                      <wls:context-root>persistence</wls:context-root>
                      <wls:container-descriptor>
<wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>                      
                      </wls:container-descriptor>
</wls:weblogic-web-app>

这是我的道class: @无状态 public class PersonEjb {

@PersistenceContext
private EntityManager entityManager;
private static final Logger logger =Logger.getLogger("PersonEjb");
public void save(Person person) {
    entityManager.persist(person);
    logger.info("persisted");
}
}

这是 servlet:

@WebServlet("/Serv1")
public class Serv1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @EJB
    private PersonEjb pe;
    public Serv1() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Person p = new Person();
        p.setAge(25);
        p.setFirstName("hamidreza");
        p.setLastName("abroshan");
        pe.save(p);
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

} 您可以在 github.

下载该项目

谢谢分配。

作为OpenJpa documentation Section 2, “ Integrating with the Transaction Manager ” 说,我应该将 <property name="openjpa.TransactionMode" value="managed"/> 添加到 persistence.xml。
通过这个 属性,OpenJpa 明白应该使用容器的托管事务。
对于weblogic,我不需要配置“openjpa.ManagedRuntime”属性.
并根据 Section 2.1, “ Managed and XA DataSources ”. “当使用托管数据源时,您还应该配置第二个非托管数据源,OpenJPA 可以使用它来执行独立于全局事务的任务。这些任务中最常见的是更新序列 table OpenJPA 用于生成唯一数据存储标识对象的主键值。“
注意:如果你没有设置AutoStrategy id generation,并且你之前创建了你的数据库的schema和sequence,并且remove

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/> 

从您的 persistence.xml,您不需要第二个 none jta 数据源。

所以,这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
            version="2.0">
            
            <persistence-unit name="batch" transaction-type="JTA" > 
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl </provider>
    <jta-data-source>jdbc/new</jta-data-source>
    <class>com.smartsoft.persistence.Person</class>
    <validation-mode>NONE</validation-mode>
      <properties>
    <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
      <property name="openjpa.Log" value="DefaultLevel=TRACE"/>
      <property name="openjpa.TransactionMode" value="managed"/>
      <property name="openjpa.ConnectionFactoryMode " value="managed"/>
     <property name="openjpa.Connection2UserName" value="app"/>
    <property name="openjpa.Connection2Password" value="app"/>
    <property name="openjpa.Connection2URL" value="jdbc:derby://localhost:1527/testdb;create=false"/>
    <property name="openjpa.Connection2DriverName" value="org.apache.derby.jdbc.ClientDriver"/>
    </properties>
  </persistence-unit>
</persistence>