本文介绍了为什么这里的交易被视为两个单独的交易?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我在弹簧控制器中有以下方法

Here I have below method in spring controller

@Override
@Transactional
public Customer updateCustomer(Custom customer) throws Exception {
.....
depatmentService.updateDepartment(department);
..........


}

我在帮助器类中有以下方法

I have below method in helper class

@Override
@Transactional(readOnly = false)
public Department updateDepartment(Department department) throws Exception {
.....

}

我正在观察的是,只要线程从方法updateDepartment中出来,该方法下的更改就会被提交.我不知道 为什么 ?默认情况下,传播是Propagation.REQUIRED,这意味着Support a current transaction, create a new one if none exists. 然后方法updateDepartment的事务与方法updateCustomer

What i am observering is as soon as thread comes out of method updateDepartment, changes under that method getting committed. I am not sure why ? As default propagation is Propagation.REQUIRED which means that Support a current transaction, create a new one if none exists. Then how come transaction for method updateDepartment is separate from method updateCustomer

我在Spring事务中使用JPA(休眠实现).我也没有看到在xml中显式设置行为propagation

I am using JPA( hibernate implementation) with spring transaction. Also i don't see explicitly setting behaviour propagation in xml

春季配置中交易管理的相关部分

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>


<tx:annotation-driven transaction-manager="txManager" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="mappingResources" value="META-INF/custom-mappings.hbm.xml" />
  <property name="packagesToScan" value="com...,   ...Other packages" />
  <property name="jpaVendorAdapter">
    <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
         ...........
    </bean>
  </property>
  <property name="jpaProperties">
    <props>
      <prop key="org.hibernate.envers.default_schema">${jdbc.audit.schema}</prop>
      .........
      <prop key="hibernate.session_factory_name">SessionFactory</prop>
    </props>
  </property>
  <property name="jpaPropertyMap">
    <map>
      <entry key="javax.persistence.validation.factory">
        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
      </entry>
    </map>
  </property>
</bean>

我也有与控制器相关的配置文件

I have configuration file related to controller also

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

推荐答案

在控制器方法上具有@Transactional批注很罕见.将事务划分置于服务级别的常用用法.

It is uncommon to have @Transactional annotations on controller methods. The common usage it to put transaction demarcation at service level.

事务控制器是可能的,但有一些警告.首先,Spring事务管理基于Spring AOP,默认情况下使用JDK代理.它在服务级别上运行良好,因为服务是作为接口注入到控制器中的.但是不会将控制器作为接口注入,因此它将无法正常工作,并且您必须将类目标代理与CGLib代理一起使用才能正常工作.据报道,使用控制器实现与JDK代理的接口可以在某些Spring版本上运行,而在其他版本上则失败:请参见另一篇文章

Transactional controllers are possible but have some caveats. First, Spring transaction management is based on Spring AOP and uses JDK proxies by default. It works fine at service level, because services are injected as interfaces in controller. But controllers are not injected as interfaces, so it will not work and you will have to use class target proxying with CGLib proxies for it works. Having controller implementing interfaces with JDK proxies has been reported to work on some Spring versions and fail on other: see this other post

TL/DR:除非您真的不能将事务划分置于服务级别而不是控制器级别.

TL/DR: unless you really cannot put transaction demarcation at service level and not at controller level.

这篇关于为什么这里的交易被视为两个单独的交易?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:04