gpt4 book ai didi

java - 为什么这里的交易被视为两个单独的交易?

转载 作者:行者123 更新时间:2023-12-01 11:16:23 25 4
gpt4 key购买 nike

这里我在 spring Controller 中有以下方法

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


}

我在辅助类中有以下方法

@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 分开

我正在使用 JPA( hibernate 实现)和 spring 事务。另外,我没有看到显式设置行为 propagation在 XML 中

spring 配置中事务管理的相关部分

<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>

我还有与 Controller 相关的配置文件

<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">

最佳答案

Controller 方法上有 @Transactional 注释的情况并不常见。常见用法是将事务划分在服务级别。

事务 Controller 是可能的,但有一些注意事项。首先,Spring事务管理基于Spring AOP,默认使用JDK代理。它在服务级别工作得很好,因为服务是作为 Controller 中的接口(interface)注入(inject)的。但 Controller 不会作为接口(interface)注入(inject),因此它不起作用,您必须将类目标代理与 CGLib 代理一起使用才能起作用。据报道,使用 JDK 代理实现接口(interface)的 Controller 在某些 Spring 版本上可以工作,而在其他版本上则失败:请参阅 this other post

TL/DR:除非您确实无法将事务划分放在服务级别而不是 Controller 级别。

关于java - 为什么这里的交易被视为两个单独的交易?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31784818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com