您的位置:首页 > 编程语言 > Java开发

Spring和Mybatis整合---注解实现AOP事务

2017-12-27 21:40 465 查看
XML配置如下:

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

<!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="service,aspect" />

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
</bean>

<!-- 配置基于注解的声明式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

声明式事务类:

package service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.IAccountDao;
import entity.Account;
//isolation处理事务隔离级别,propagation事务的传播特性
@Transactional(isolation=Isolation.DEFAULT,readOnly=false,propagation=Propagation.REQUIRED)
@Service
public class AccountService {
@Autowired
private IAccountDao iAccountDao;
/*
* 1号给2号100块钱,
*/
//发生数字异常不会滚
@Transactional(noRollbackFor=ArithmeticException.class,readOnly=true)
public void buy() {
Account account1=iAccountDao.findAccount(1);
Account account2=iAccountDao.findAccount(2);
//修改1号钱
account1.setMoney(account1.getMoney()-100);
iAccountDao.updateAccount(account1);
//这是一个异常!
int a=1/0;
//修改2号钱
account2.setMoney(account2.getMoney()+100);
iAccountDao.updateAccount(account2);

}
}


--编程式事务和声明式事务


编程式事务:
try{

accountDao.updateAccount(account1);

accountDao.updateAccount(account2);

commit()

}catch(){

rollback();

}finally{

}

给每个业务方法当中某几句代码当成一个事务去trycatch处理.

坏处:重复代码处理,代码复杂.好处:粗粒级别控制.
声明式事务:@Transactional
将重复的处理事务的代码抽取出来,  基于AOP

好处:程序员只需要关注业务代码,而无视事务处理.缺点:粗粒级别控制.

事务的一些概念:

--事务特性:事务特性ACID 原子性,一致性,隔离性,持久性
--事务隔离级别:
Read uncommitted:可能会读到另一个事务没有提交的数据  ,脏读

         Read committed:可能会读到另一个事务已经提交的update的数据,不可重复读

         Read Repeatable:可能会读到另一个事务已经提交的insert的数据,虚读  mysql默认
Serizable:不会发生脏读,不可重复读,虚读
--事务的传播特性:
PROPAGATION_REQUIRED:支持当前的事务,如果不存在就创建一个。如果A有事务,B使用A的事务,如果A没有事务,B就开启一个新的事务(保证A和B在同一个事务中)。
PROPAGATION_SUPPORTS:支持当前事务,如果不存在就不使用事务。A、B:如果A有事务,B使用A的事务,如果A没有事务,B就不使用事务。
PROPAGATION_MANDATORY:支持当前的事务,如果不存在就会抛出异常。A、B: 如果A有事务,B使用A的事务,如果A没有事务则抛出异常。
PROPAGATION_REQUIRES_NEW:如果有事务存在,则挂起当前事务,创建一个新的事务。A、B: 如果A有事务,B将A的事务挂起,重新创建一个新的事务(保证A和B不在同一个事务中)。
PROPAGATION_NOT_SUPPORTED:以非事务的方式运行,如果有事务存在,挂起当前的事务。A、B:非事务的方式运行,A有事务就会挂起当前的事务。
PROPAGATION_NEVER:以非事务的方式运行,如果有事务存在则抛出异常。 
PROPAGATION_NESTED:如果当前事务存在,则嵌套事务执行。它基于SavePoint(保存点)技术。A、B: A有事务,A执行事务之后的内容保存到SavePoint,若B事务有异常,由自己设置事务提交还是回滚。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring aop 事务