【发布时间】:2015-10-25 09:56:20
【问题描述】:
这里我在spring控制器中有以下方法
@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(休眠实现)和 spring 事务。我也没有看到在 xml 中明确设置行为 propagation
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>
我也有控制器相关的配置文件
<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">
【问题讨论】:
-
请添加
updateDepartment正文。 -
添加
readOnly=false更新客户。 -
有人可以将此问题标记为重复吗?类似问题:stackoverflow.com/questions/2865055/…
-
是的,我的意思是“这是唯一的……”。控制器由 spring 上下文处理,该上下文是配置事务管理的根上下文的子上下文。所以事务管理只适用于根上下文中的bean,控制器上的事务注解因此不起作用,因为控制器是由子上下文处理的。