当前位置: 首页 > 面试题库 >

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

张宣
2023-03-14
问题内容

我需要用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


问题答案:

感谢 @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>


 类似资料:
  • 我正在使用Guice注入EntityManager。当我提交注入实体管理器的操作时,BD端没有发生任何事情:没有事务通过!!!你能帮我弄清楚发生了什么吗? 这是我的代码: 网状物xml Inject torListener类: 持久性模块类: GuiceModule类: RestModule类: 公共类RestModule扩展了JerseyServletModule{ 最后是网络服务(jeresy

  • 我正在用JPA和Hibernate做一些简单的测试,当我不希望的时候,我碰巧关闭了EntityManager。 这是我的测试主: DB助手: 实体和对应的DAO: 在商店里,当开始交易时,我得到以下信息: 未执行关机方法。为什么实体管理器关闭? 为了完整起见,以下是persistence.xml:

  • 问题内容: 我正在将DAO层从使用Hibernate API更改为使用纯JPA API实现的过程中。似乎推荐的方法是使用实​​体管理器中的createNamedQuery。命名查询存储在模型/实体类的注释中。这对我来说毫无意义。为什么要在模型对象中定义JPA查询,却在DAO中使用它们。仅在DAO本身中使用createQuery并在DAO中定义查询,甚至在DAO本身中定义命名查询,是否更有意义? 对

  • 问题内容: 我正在使用Struts 1.3 + JPA(使用Hibernate作为持久性提供程序)开发一个简单的“ Book Store”项目。我不能切换到Spring或任何其他更复杂的开发环境(例如Jboss),也不能使用任何特定于Hibernate的技术(例如,类)。 考虑到我处于JSE环境中,我需要显式管理整个EntityManager的生命周期。 该实体被定义如下: 我定义了三个类,分别负

  • 我从我的Spring项目中删除了Spring Roo,这是不必要的,而且在eclipse STS中构建花了很多时间。我执行了一个推入,我的实体对象现在包含了旧的Roo文件的源代码。 几乎没有什么变化,包括PersistentContext管理,这看起来很奇怪。事实上,我注意到,每次创建查询时,我都需要实例化实体类以获得EntityManager? 在我的实体类中,我有

  • 如何使用Spring JPA注入EntityManager对象 我使用的是spring上下文 我的dao是,注入EntityManager 我的persistence.xml 所以我收到以下错误: