没有可用于当前线程的实际事务的EntityManager-无法可靠地处理“持久”

时间:2019-09-10 08:39:34

标签: java spring transactions aop aspectj

在对部署在Tomcat上的Web服务进行插入操作时,我面临以下异常

  

javax.persistence.TransactionRequiredException:否EntityManager具有   当前线程可用的实际事务-无法可靠地   处理“持续”呼叫

我的spring-jpa.xml如下

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

    <!-- ========================= RESOURCE DEFINITIONS ========================= -->

    <!-- Activates a load-time weaver for the context. Any bean within the context that implements LoadTimeWeaverAware 
        (such as LocalContainerEntityManagerFactoryBean) will receive a reference to the autodetected load-time weaver. -->



    <context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />



    <bean id="entityManagerFactoryReadOnly" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="ReadOnlyBasePersistenceUnit" />
    </bean>

    <bean id="entityManagerFactoryReadWrite" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="ReadWriteBasePersistenceUnit" />
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" />

    <bean id="transactionManagerReadOnly" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryReadOnly" />
        <property name="defaultTimeout" value="90" />
    </bean>

    <bean id="transactionManagerReadWrite" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryReadWrite" />
        <property name="defaultTimeout" value="90" />
    </bean>


    <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

    <!-- Instruct Spring to perform declarative transaction management automatically on annotated classes. -->
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManagerReadWrite"/>
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManagerReadOnly"/>

    <!-- Post-processor to perform exception translation on @Repository classes (from native exceptions such 
         as JPA PersistenceExceptions to Spring's DataAccessException hierarchy). -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

dependecncies.gradle如下

    compile (
        'commons-lang:commons-lang:2.2',
        'javax.xml:jsr173:1.0',
        'commons-httpclient:commons-httpclient:3.1',
        'javax.servlet:servlet-api:2.5',
        'regexp:regexp:1.3',
        'org.aspectj:aspectjrt:1.8.4',
        'org.aspectj:aspectjweaver:1.8.4',
        'org.springframework:spring-aspects:5.0.0.RELEASE',
        'org.springframework:spring-jdbc:5.0.0.RELEASE',
        'org.springframework:spring-jms:5.0.0.RELEASE',
        'org.springframework:spring-orm:5.0.0.RELEASE',
        'org.springframework:spring-oxm:5.0.0.RELEASE',
        'org.springframework:spring-web:5.0.0.RELEASE',
        'org.springframework:spring-webmvc:5.0.0.RELEASE',
        'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
        'javax.persistence:javax.persistence-api:2.2',
        'org.eclipse.persistence:eclipselink:2.5.2' 
}

服务类别如下

class Facade impelments Ifacade {
@Autowired
private MyDao dao

@Override
@Transactional
public CreateRequestResponse createRequests(List<BusinnessObject> bobject) {
          this.dao.createRequests(bobject)
} 

}

我的persistence.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">

    <persistence-unit name="ReadOnlyBasePersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <non-jta-data-source>jdbc/DS_RW</non-jta-data-source>
        <class>com.temp.request.model.dataobject.RqstOne</class>
        <class>com.temp.request.model.dataobject.RqstTwo</class>
        <class>com.temp.request.model.dataobject.RqstThree</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="eclipselink.target-server" value="com.temp.request.tomcat.TomcatPlatform"/>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform" />
            <property name="eclipselink.logging.level" value="WARNING" />
            <property name="eclipselink.persistence-context.flush-mode" value="COMMIT" />
            <property name="eclipselink.persistence-context.close-on-commit" value="true" />
            <property name="eclipselink.cache.shared.default" value="false" />
        </properties>
    </persistence-unit>

    <persistence-unit name="ReadWriteBasePersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <non-jta-data-source>jdbc/DS_RW</non-jta-data-source>
        <class>com.temp.request.model.dataobject.RqstOne</class>
        <class>com.temp.request.model.dataobject.RqstTwo</class>
        <class>com.temp.request.model.dataobject.RqstThree</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="eclipselink.target-server" value="com.temp.request.tomcat.TomcatPlatform"/>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.Oracle11Platform" />
            <property name="eclipselink.logging.level" value="WARNING" />
            <property name="eclipselink.persistence-context.flush-mode" value="COMMIT" />
            <property name="eclipselink.persistence-context.close-on-commit" value="true" />
            <property name="eclipselink.cache.shared.default" value="false" />
        </properties>
    </persistence-unit>

</persistence>

0 个答案:

没有答案