spring+mybatis使用事务管理不生效原因分析

spring+mybatis使用事务管理不生效原因

  1. 检查spring的配置信息是否配置正确
<!-- 配置事务的通知:(事务的增强) -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 
				propagation :事务的传播行为
				isolation   :事务的隔离级别 
				read-only   :只读
				rollback-for:发生哪些异常回滚
				no-rollback-for:发生哪些异常不回滚
				timeout     :过期信息
			 -->
			<tx:method name="transfer" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置切面  -->
	<aop:config>
		<!-- 配置切入点 -->
		<aop:pointcut id="pointcut"
			expression="execution(* com.xinrui.hospital.service.*.*(..))" />
		<!-- 配置切面 -->
		<aop:advisor pointcut-ref="pointcut" advice-ref="txAdvice" />
	</aop:config>

其中最容易出错的点是
expression="execution(* com.xinrui.hospital.service..(…))"的写法,上述写法是正确的写法。

2.其次是在service层对于要管理的方法不要使用try-catch来捕获异常信息

package com.xinrui.hospital.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.xinrui.hospital.bean.Account;
import com.xinrui.hospital.dao.IAccountDao;
import com.xinrui.hospital.service.IAccountService;

/**
 * 
 * @ClassName: AccountServiceImpl
 * @Description: 转账服务实现类
 * @author 梁志成
 * @date 2016年3月22日 下午5:26:36
 *
 */
@Service(value = "accountService")
public class AccountServiceImpl implements IAccountService {

	@Resource(name = "accountDao")
	private IAccountDao accountDao;

	/**
	 * 
	 * @Title: transfer
	 * @Description: 转账
	 * @param @param out 转出账号
	 * @param @param in 转入账号
	 * @param @param money 转账金额
	 * @return void
	 */
	public boolean transfer(String out, String in, Double money) {

		if (StringUtils.isNotEmpty(out) && StringUtils.isNotEmpty(in)) {
			Account outAccount = new Account();
			outAccount.setMoney(money);
			outAccount.setName(out);
			accountDao.outMoney(outAccount);
			int i = 1 / 0;
			Account inAccount = new Account();
			inAccount.setMoney(money);
			inAccount.setName(in);
			accountDao.inMoney(inAccount);
		}

		return false;

	}
}

上述是正确的配置。

3.确保事务交由spring管理

	<!-- 配置由spring扫描的注解 -->
	<context:component-scan base-package="com.xinrui.hospital"
		use-default-filters="true">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>
	<!-- 配置由Spring-mvc扫描的注解 -->
	<context:component-scan base-package="com.xinrui.hospital.action"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
	</context:component-scan>

上述是正确的配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两只橙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值