WildFly v9无法使用SynchronizationType创建EntityManager,因为PersistenceUnit配置了资源本地事务错误

时间:2016-01-12 14:13:55

标签: java-ee wildfly application-server

在WildFly v9上,我尝试使用容器管理的事务。但是,当我尝试持久化时,服务器会抛出此错误:

15:55:54,501 ERROR [stderr] (default task-21) java.lang.IllegalStateException: Unable to create EntityManager with SynchronizationType because PersistenceUnit is configured with resource-local transactions.

但是在我的persistence.xml文件中,我使用JTA进行数据库操作:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="HospitalAutomation" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/HospitalAutomation</jta-data-source>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Klinikler</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Klinikyerleri</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Doktorlar</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Hastaneler</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Uygunrandevular</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Ilceler</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Iller</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Patients</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Randevusaatleri</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.Takenappointments</class>
    <class>com.ilkgunel.hastaneotomasyonu.entity.LocalDateConverter</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <validation-mode>NONE</validation-mode>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/HospitalAutomation?useUnicode=true&amp;characterEncoding=UTF-8"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

我在Java类中使用@PersistenceContext注释来管理EntityManager生命周期,我的Java类是这样的:

/*


* To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import com.ilkgunel.hastaneotomasyonu.entity.Patients;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ilkaygunel
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SavePatientSessionBean implements SavePatientSessionBeanLocal {

    @PersistenceContext(unitName = "HospitalAutomation")
    private EntityManager em;

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String savePatient(Patients patients) {
        try 
        {
            //em.getTransaction().begin();
            em.persist(patients);
            //em.getTransaction().commit();
            return "Bilgileriniz Kaydedildi. Sisteme Giriş Yapıp Randevu Alabilirsiniz";
        } catch (Exception e) {
            System.err.println(e);
            return "Bilgilerin Kaydı Sırasında Bir Hata Meydana Geldi!";
        }
    }

}

1 个答案:

答案 0 :(得分:1)

奥基。我找到了解决这个问题的方法。 首先在项目中复制eclipselink.jar。对我来说,它是eclipselink-2.5.2.jar。然后将此* .jar文件粘贴到wildfly文件夹/ modules / system / layer / base / org / eclipse / persistence / main /。执行此操作后,打开同一文件夹(main)中的modules.xml文件并进行如下编辑:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~ JBoss, Home of Professional Open Source.
  ~ Copyright 2011, Red Hat, Inc., and individual contributors
  ~ as indicated by the @author tags. See the copyright.txt file in the
  ~ distribution for a full listing of individual contributors.
  ~
  ~ This is free software; you can redistribute it and/or modify it
  ~ under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ This software is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with this software; if not, write to the Free
  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  -->

<!-- Represents the EclipseLink module  -->
<module xmlns="urn:jboss:module:1.3" name="org.eclipse.persistence">
    <resources>
        <resource-root path="jipijapa-eclipselink-1.0.1.Final.jar"/>
        <resource-root path="eclipselink-2.5.2.jar">
           <filter>
                   <exclude path="javax/**" />
           </filter>
        </resource-root>
    </resources>

    <dependencies>
        <module name="asm.asm"/>
        <module name="javax.api"/>
        <module name="javax.annotation.api"/>
        <module name="javax.enterprise.api"/>
        <module name="javax.persistence.api"/>
        <module name="javax.transaction.api"/>
        <module name="javax.validation.api"/>
        <module name="javax.xml.bind.api"/>
        <module name="org.antlr"/>
        <module name="org.apache.commons.collections"/>
        <module name="org.dom4j"/>
        <module name="org.javassist"/>
        <module name="org.jboss.as.jpa.spi"/>
        <module name="org.jboss.logging"/>
        <module name="org.jboss.vfs"/>
    </dependencies>
</module>

一切都很好。现在运行你的项目。