0 0

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

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


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");
		}
	}

}

测试代码如下
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;
	}
	
}

按照我的理解,ProductService.innerSave()方法抛出了runtime exception,那么结果应该是Order被保存,Product被回滚,但实际测试结果显示两个对象都被保存(debug显示save point被创建,并被rollback), why???
2009年7月08日 10:39

3个答案 按时间排序 按投票排序

0 0

采纳的答案

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

2009年7月09日 11:29
0 0

/**
* Execute within a nested transaction if a current transaction exists,
* behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.
* <p>Note: Actual creation of a nested transaction will only work on specific
* transaction managers. Out of the box, this only applies to the JDBC
* DataSourceTransactionManager when working on a JDBC 3.0 driver.
* Some JTA providers might support nested transactions as well.
* @see org.springframework.jdbc.datasource.DataSourceTransactionManager
*/
NESTED(TransactionDefinition.PROPAGATION_NESTED);

参加源码里面的javadoc

2009年7月09日 14:06
0 0

你引用了错误的system.platformTransactionManager

2009年7月08日 14:47

相关推荐

Global site tag (gtag.js) - Google Analytics