PROPAGATION_REQUIRED,PROPAGATN_REQUIRES_NEW,PROPAGATION_NESTED区别

PROPAGATION_REQUIRED嵌套时,不管是否用try catch,只要里外二个事务有一个出错,都会因rollback。因为是同一个事务范围。

PROPAGATN_REQUIRES_NEW被嵌套时,外面的事务不会影响里面的事务。如果里面的事务用了try catch,那么出错外面不会rollback,里面rollback。如果没用,里面的抛出错,会导致里外的事务都rollback。

PROPAGATION_NESTED最有意思。它被嵌套时,实际上是起的一个子事务。外面的事务rollback了,里面的也rollback。如果里面的事务用了try catch,那么出错外面不会rollback,里面rollback。如果没用,里面的抛出错,会导致里外的事务都rollback。

发布了18 篇原创文章 · 获赞 1 · 访问量 1万+
展开阅读全文

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

07-08

一年前记得做过Nested传播级别的测试,当时结果是同Spring文档上介绍的一致的,即通过创建Savepoint实现嵌套事务,达到内层事务若抛出异常(unchecked exception)则回滚到savepoint处,但不影响外层事务;外层事务的回滚会一起回滚内层事务; 现在碰到的问题是,内层事务抛出unchecked exception,按照上述逻辑应该会回滚内层事务提交的数据,即便外层事务提交的情况下,但测试结果并非如此,外层事务一提交,内层方法创建的数据仍然被提交(debug显示内层事务已调用rollback),不知道是哪地方出了错误,现将测试代码贴上,请大家帮忙分析! [code="java"] <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.platformTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="system.sessionFactory"/> <property name="nestedTransactionAllowed" value="true"/> </bean> --> <!-- --> <bean id="system.platformTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="system.datasource"/> </bean> <bean id="system.propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean> </beans> [/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??? 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览