一尘不染

使用JtaTransactionManager时,为什么未在Spring Service中提交使用EntityManager的JPA更改?

hibernate

我需要用org.springframework.transaction.jta.JtaTransactionManager我的TransactionManager春天Service。但是,它不对JPA实体进行任何更改。我知道,如果我使用JpaTransactionManager它会起作用。但是,我需要JtaTransactionManager。因此,建议您不要使用JpaTransactionManager。我的spring班Service是:

package testspring.view;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import testspring.model.Regions;


@Service
public class HelloBS {
    @PersistenceContext
    private EntityManager entityManager;

    public HelloBS() {
        super();
    }

    @Transactional()
    public void doSomething() {
        Regions region = new Regions();
        region.setRegionName("Antarctica");
        entityManager.persist(region);
    }
}

我的Spring xml配置是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="        
       http://www.springframework.org/schema/beans        
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
       http://www.springframework.org/schema/tx        
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        
       http://www.springframework.org/schema/context        
       http://www.springframework.org/schema/context/spring-context-3.2.xsd        
       http://www.springframework.org/schema/mvc        
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
       http://www.springframework.org/schema/jee         
       http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">
    <context:component-scan base-package="testspring.view"/>
    <context:annotation-config/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <mvc:default-servlet-handler/>
    <bean id="dataSource" name="dataSource"
          class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/hrDS"/>
        <property name="resourceRef" value="true"/>
    </bean>
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="testspring.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="javax.persistence.validation.mode">AUTO</prop>
                <prop key="hibernate.archive.autodetection">class</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.transaction.flush_before_completion">true</prop>
                <prop key="hibernate.transaction.auto_close_session">true</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager"></bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

如何在使用提交的JPA实体中进行更改JtaTransactionManager


阅读 264

收藏
2020-06-20

共1个答案

一尘不染

感谢 @Bond-Java Bond ,解决方案是执行以下步骤:

  • 设置jtaDataSourcedataSource
  • 使用<tx:jta-transaction-manager/>代替<tx:annotation-driven transaction-manager="transactionManager"/>
  • 添加<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>org.hibernate.service.jta.platform.internal软件包中有不同的类,可用于不同的Application Server。

因此,最终的Spring xml配置为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jee="http://www.springframework.org/schema/jee"

       xsi:schemaLocation="        
       http://www.springframework.org/schema/beans        
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
       http://www.springframework.org/schema/tx        
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        
       http://www.springframework.org/schema/context        
       http://www.springframework.org/schema/context/spring-context-3.2.xsd        
       http://www.springframework.org/schema/mvc        
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
       http://www.springframework.org/schema/jee         
       http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">
    <context:component-scan base-package="testspring.view"/>
    <context:annotation-config/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <mvc:default-servlet-handler/>
    <bean id="dataSource" name="dataSource"
          class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/hrDS"/>
        <property name="resourceRef" value="true"/>
    </bean>
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    <bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jtaDataSource" ref="dataSource"/>
        <property name="packagesToScan" value="testspring.model"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>        
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="javax.persistence.validation.mode">AUTO</prop>
                <prop key="hibernate.archive.autodetection">class</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.transaction.flush_before_completion">true</prop>
                <prop key="hibernate.transaction.auto_close_session">true</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
                <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>
            </props>
        </property>
    </bean>
    <tx:jta-transaction-manager/>

</beans>
2020-06-20