spring+springMVC事务不回滚

本次项目中,使用的框架是spring+springMVC,数据库使用的是springJDBC操作,数据源使用的是dynamicDataSource。项目中,事务的配置是基于tx命名空间配置,配置信息如下:

<aop:config expose-proxy="true">
		<aop:pointcut id="txPointcut" expression="execution(* *..service*..*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
	</aop:config>

	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dynamicDataSource" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="merge*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />

			<tx:method name="put*" read-only="true" />
			<tx:method name="query*" read-only="true" />
			<tx:method name="use*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="count*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="list*" read-only="true" />

			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

切面切到service层,但在实际中,无法service抛出什么异常,事务都不回滚,试了网上说的人为抛出RuntimeException、不用try-catch处理异常、配置事务回滚的异常类等,均无法使事务回滚。最近在搜索这个问题时,在http://icanfly.iteye.com/blog/778401看到了关于解决spring事务不回滚的文章,文章中说:

<!-- 引用http://icanfly.iteye.com/blog/778401中的内容 -->

这个问题很经典了
在主容器中(applicationContext.xml),将Controller的注解排除掉
<context:component-scan base-package="com">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

而在springMVC配置文件中将Service注解给去掉
<context:component-scan base-package="com">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
  </context:component-scan>

因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。

还有一种方式是将service层改用xml配置,其实这样做也是变相的让springmvc无法扫描service,而只能依赖父窗口也就是ServletContextListener来进行初始化,这样同样被赋予了事务性。


果然,改了配置文件后,事务就可以正常回滚了。






评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值