【问题标题】:Spring transaction management for execution service methods from same service来自同一服务的执行服务方法的 Spring 事务管理
【发布时间】:2011-02-09 19:44:35
【问题描述】:

例如,我们有 bean 服务,它有两种方法 其中一个(testA)执行另一个(testB)。

public class TestServiceImpl implements TestService {
...
public void testA() throws Exception {
    ...     
    try {
        this.testB();
    catch(Exception e)
    {
        ...
    }
    ...
}
public void testB() throws Exception {
    ...
}

}

方法 testB 使用 PROPAGATION_REQUIRES_NEW 事务属性定义, 方法 testA 使用 PROPAGATION_REQUIRED 定义。

<bean id="TestService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="target" ref="TestServiceTarget"/>
    <property name="transactionManager" ref="transactionManager"/>
    <property name="transactionAttributes">
        <props>             
            <prop key="testB*">PROPAGATION_REQUIRES_NEW, ISOLATION_READ_COMMITTED, -Exception</prop>
            <prop key="*">PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception</prop>
        </props>
    </property>
</bean>

然后我们执行这个服务的testA方法。 在日志中我们可以看到然后为方法 test 创建了一个新事务,什么都可以。 但是当从methodA执行testB方法时, 在日志中,我们有信息表明正在使用实际事务 而不是暂停实际并创建另一个 (就像在配置中定义的那样)。

春天的正常行为是这样吗? 当我们在同一服务的其他方法中执行服务方法时, spring 省略了这个方法的事务配置 (例如方法 testB 的示例) 并始终使用 PROPAGATION_REQUIRED 属性?

另外让我们说当方法 testB 来自其他服务时 并且此方法的配置相同(PROPAGATION_REQUIRES_NEW) 一切似乎都很好(新事务已创建,实际已暂停)。

作为事务管理器使用 WebSphereTransactionManagerFactoryBean (spring 2.5)。

谢谢

【问题讨论】:

    标签: spring transactions


    【解决方案1】:

    是的,这是正常行为。

    请参阅7.6.1 Understanding AOP proxies 以获取解释和一些解决方法。另见Spring - @Transactional - What happens in background?。

    【讨论】:

    猜你喜欢
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2021-11-18
    • 1970-01-01
    • 2011-05-19
    • 2021-12-05
    相关资源
    最近更新 更多