【问题标题】:Spring Call REQUIRED method from REQUIRED_NEW method来自 REQUIRED_NEW 方法的 Spring 调用 REQUIRED 方法
【发布时间】:2015-04-24 07:17:04
【问题描述】:

我有如下 bean 条目

<bean id="inspectReqImpl"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>
                <prop key="activateRequest">PROPAGATION_REQUIRES_NEW</prop>                             
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
        <property name="target">
            <bean
                class="com.test.InspectRequestImpl">                
            </bean>
        </property>
</bean>

<bean id="inspectReqProcess"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="transactionManager" />
        <property name="transactionAttributes">
            <props>             
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
        <property name="target">
            <bean
                class="com.test.InspectRequestProcessImpl">             
            </bean>
        </property>
</bean>

当我调用 com.test.InspectRequestImpl.activateRequest() 方法时,Spring 将创建新事务

public void activateRequest()
{

Calling method of InspectRequestProcessImpl(ex: processRequest())
For InspectRequestProcessImpl we have defined all methods as PROPAGATION_REQUIRED

}

因此,如果任何异常(已检查或未检查)出现在方法 (InspectRequestProcessImpl.processRequest()) 中,其事务传播为 REQUIRED,那么对具有事务传播 REQUIRED_NEWInspectRequestImpl.activateRequest() 有何影响

  • 在这种情况下弹簧的表现如何。它会提交InspectRequestImpl.activateRequest() 还是回滚?
  • 它会只是回滚InspectRequestProcessImpl.processRequest() 的内容还是会引发异常?

【问题讨论】:

    标签: java spring hibernate transactions spring-transactions


    【解决方案1】:

    默认行为是在检查异常后提交,在未检查异常后回滚。

    如果您希望在检查异常后进行回滚,请将回滚规则添加到您的事务规范中。 对一般异常使用“PROPAGATION_REQUIRED,-Exception”或对特定异常使用“PROPAGATION_REQUIRED,-MyException”。

    如果您的方法声明抛出异常,则将抛出异常,否则不会。 Spring 不会传播异常...您可以尝试/捕获异常并且什么也不做。

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2014-07-10
      • 2012-05-27
      • 2015-10-10
      • 1970-01-01
      相关资源
      最近更新 更多