iteye_18065 2009-07-08 10:39
浏览 807
已采纳

关于Spring事务传播级别PROPAGATION_NESTED的问题

一年前记得做过Nested传播级别的测试,当时结果是同Spring文档上介绍的一致的,即通过创建Savepoint实现嵌套事务,达到内层事务若抛出异常(unchecked exception)则回滚到savepoint处,但不影响外层事务;外层事务的回滚会一起回滚内层事务;
现在碰到的问题是,内层事务抛出unchecked exception,按照上述逻辑应该会回滚内层事务提交的数据,即便外层事务提交的情况下,但测试结果并非如此,外层事务一提交,内层方法创建的数据仍然被提交(debug显示内层事务已调用rollback),不知道是哪地方出了错误,现将测试代码贴上,请大家帮忙分析!
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="orderDao" class="com.liuj.dao.hibernate.OrderHibernateDao" autowire="byType"/>    
<bean id="orderService" class="com.liuj.service.impl.OrderServiceImpl" autowire="byName"/>

<bean id="productDao" class="com.liuj.dao.hibernate.ProductHibernateDao" autowire="byType"/>    
<bean id="productService" class="com.liuj.service.impl.ProductServiceImpl" autowire="byName"/>


<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
        <list>                          
            <!-- beanNames for system.deployTargetAdvisor -->
            <value>orderService</value>
            <value>productService</value>
        </list>
    </property>
    <property name="interceptorNames">
        <list>
            <value>system.transactionAdvisor</value>
        </list>
    </property>
</bean> 

<bean id="system.transactionAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
    <property name="advice" ref="system.transactionInterceptor"/>
    <property name="pointcut" ref="system.transactionPointcut"/>        
</bean>

<bean id="system.transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager"><ref bean="system.platformTransactionManager"/></property>
    <property name="transactionAttributes"> 
        <props> 
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> 
            <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>  
            <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
            <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>     
            <prop key="innerSave*">PROPAGATION_NESTED</prop>
            <prop key="*">PROPAGATION_REQUIRED</prop>
        </props> 
    </property>         
</bean> 
<bean id="system.transactionPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
    <property name="patterns">
        <list>
            <value>com.liuj.service.OrderService.*</value>
            <value>com.liuj.service.ProductService.*</value>
        </list>
    </property>
</bean>     

<!-- Hibernate SessionFactory -->
<bean id="system.sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="system.datasource"/>
    <property name="annotatedClasses">
        <list>
            <value>com.liuj.model.Order</value>
            <value>com.liuj.model.Product</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
            <prop key="hibernate.max_fetch_depth">3</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>


<!-- Local DataSource that works in any environment --> 
<!-- 
<bean id="system.datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>    
    <property name="initialPoolSize" value="${jdbc.initialPoolSize}" /> 
    <property name="minPoolSize" value="${jdbc.minPoolSize}" />
    <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
    <property name="idleConnectionTestPeriod" value="300" />
    <property name="breakAfterAcquireFailure" value="true" />
    <property name="checkoutTimeout" value="100000" />
</bean>
 -->

<bean id="system.datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<!--
-->


<bean id="system.propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property> 
</bean>


[/code]
[code="java"]

public class OrderHibernateDao extends HibernateDaoSupport implements OrderDao {

public void save(Order order) {
    getHibernateTemplate().save(order);
}

}

public class ProductHibernateDao extends HibernateDaoSupport implements ProductDao {

public void save(Product product) {
    getHibernateTemplate().save(product);
}

}
public class OrderServiceImpl implements OrderService {

private ProductService productService;

private OrderDao orderDao;

public void setOrderDao(OrderDao orderDao) {
    this.orderDao = orderDao;
}

public void setProductService(ProductService productService) {
    this.productService = productService;
}

public void outerSave(Order order) {
    orderDao.save(order);
    try {
        productService.innerSave();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}
public class ProductServiceImpl implements ProductService {

private ProductDao productDao;

public void setProductDao(ProductDao productDao) {
    this.productDao = productDao;
}

public void innerSave() {
    Product product = new Product();
    product.setName("inner product");
    productDao.save(product);
    if (true) {
        throw new RuntimeException("error happend");
    }
}

}
[/code]
测试代码如下
[code="java"]
public class OrderServiceTest extends DependencyInjectionSpringContextTests {

private OrderService orderService;

@Test
public void testSave() {
    Order order = new Order();
    order.setName("outer order");
    orderService.outerSave(order);
}

public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
}

}
[/code]
按照我的理解,ProductService.innerSave()方法抛出了runtime exception,那么结果应该是Order被保存,Product被回滚,但实际测试结果显示两个对象都被保存(debug显示save point被创建,并被rollback), why???

  • 写回答

3条回答 默认 最新

  • layer555 2009-07-09 11:29
    关注

    PROPAGATION_NESTED使用了一个单独的物理事务, 这个事务拥有多个可以回滚的保存点。这样部分回滚允许内部事务在它的作用域内触发一个回滚, 并且外部事务能够不受影响的继续。 这通常是对应于JDBC的保存点,所以只会在 JDBC 资源事务管理上起效 (具体请参见 Spring的DataSourceTransactionManager).而DataSourceTransactionManager只会对Spring Jdbc(即JdbcTemplate)或者ibatis等jdbc操作有效,你的配置里使用了hibernate的sessionFacotry来访问,所以不会有效果;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗