Spring提供了强大的事务管理功能,让我们走入spring的事务管理篇吧
前言
此篇为个人初期学习笔记,入有错误,请兄弟们指正
1. 回顾事务
a>概念
- 数据库的最小操作单元
- 逻辑上是一组操作
- 要莫同时执行成功,要莫同时执行失败------》有一个执行失败就全部失败
b>特点ACID
- 原子性(Atomicity):事务是不可分割的最小操作单元,要莫同时执行成功,要莫同时执行失败
- 一致性(Consistency):事务完成时,必须使所有的数据保持一致状态
- 隔离性(Isolation):数据库系统提供的隔离机制,保证事务在不受外部并发操作影响的独立环境下运行
- 持久性(Durability):事务一旦提交或回滚,它对数据库中数据的改变就是永久的
2. 配置事务
a>基于XML配置文件
第一步:引入aop和tx名词空间,导入aop和tx的jar
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
</beans>
第二步:配置transactionManager(spring提供Around通知)
根据不同的持久层框架,配置不同的事务管理器,事务管理器需要依赖数据源信息
<bean id="transactionManager" class="prg.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入驱动 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
第三步:配置切入点和切面
- 在spring的配置文件中,使用tx:advice配置通知信息,起始就是哪些方法需要增强,事务管理也是aop的应用
<tx:advice>
<tx:attributes>
<tx:method name="具体的方法名"/>
</tx:attributes>
</tx:advice>
- 使用aop:config来配置切入点和切面信息
<!-- 2、配置切入点,你要对哪一个方法进行增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="updateMoney"/>
</tx:attributes>
</tx:advice>
<!-- 3、配置切面-->
<aop:config>
<!-- 配置切入点表达式-->
<aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>
<!-- 切面配置,切入点和通知进行一个结合-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCat"></aop:advisor>
</aop:config>
b>基于注解
@Transactional
第一步:配置注解驱动事务管理
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入驱动 -->
<property name="dataSource"></property>
</bean>
第二步:在需要事务的方法或类上面添加@GTransactional注解
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
基于注解添加事务管理
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 配置注解扫描包:扫描com.sky包下的所有组件-->
<context:component-scan base-package="com.sky"></context:component-scan>
<!-- 核心思想:将Mybatis的所有对象全部都交给Spring管理-->
<!-- 引入数据源信息-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- location值就需要加上classpath, classpath = src-->
<property name="location" value="classpath:db.properties"></property>
</bean>
<!-- 配置数据源,数据库链接池,dbcp,c3p0-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- sqlSessionFactory:加载工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation:加载 Mybatis 的配置文件-->
<property name="configLocation" value="classpath:MybatisConfig.xml"></property>
<!-- 扫描加载所有的mapper.xml文件-->
<property name="mapperLocations" value="classpath:com/sky/mapperxml/*.xml"></property>
</bean>
<!-- sqlSession-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 配置接口的扫描包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sky.mapper"></property>
</bean>
<!-- 1、配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入驱动-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2、事务管理器的注解驱动配置,加了这个配置就能识别 Transactional 注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
c>俩种方式比较
xml方式和注解方式都在使用:
使用@Transactional注解进行事务配置,更加方便,不过配置太分散
使用XML进行事务管理,属性集中配置,便于管理和维护
3. 添加事务
-
事务一般添加到Service层(业务逻辑层)
-
用Spring进行事务管理:
编程式—原始操作,代码太长
声明式----xml、注解俩种方式
用Spring进行声明式事务管理,底层使用
AOP原理
- Spring事务管理API
spring提供了一个接口(PlatformTransacationManager),代表事务管理器这个接口争对不同框架提供不同的实现类
-
在类或方法上面添加注解
在类上添加,会争对该类所有方法都添加事务;在方法上面添加,争对当前方法添加事务
-
@Transactional
-
注解的属性:
- propagation:事务传播行为
指定一个事务方法调用另一个事务方法,被调用的方法如何运行
- REQUIRED:
默认使用该传播行为
就是说,事务A调用了事务B的方法,如果事务事务A开启了存在事务,则事务B使用该事务
如果不存在事务,事务B就会自己开启一个新的事务,执行自己的逻辑 - REQUIRED_NEW
无论当前是否存在事务,都会新建一个事务 - SUPPORTS
如果当前有事务在运行,被标记方法就去该事务中执行
如果没有事务,该方法可以不在事务中执行
- isolation:事务隔离级别
解决并发事务出现的问题:脏读、不可重复读、幻读
- READ UNCOMMITED:就是个废物,啥都解决不了的那种
- READ COMMITED:可以解决脏读问题
- REPEATABLE READ:可以解决脏读和不可重复读问题
- SERIALIZABLE(串性化):级别最高,性能最差,三种都可以解决
- timeout:超时时间
- 事务需要在指定时间进行提交,否则就进行回滚
- 默认值为-1,设置时间以秒单位进行计算
-
readOnly:是否只读
读:查询,写:增删改
readOnly默认值为false,表示可以查询,可以添加修改删除操作
设置readOnly值为true -
rollbackFor:回滚
设置出现那些异常时,进行回滚
- norollbackFor:不回滚
设置出现那些异常时,不进行回滚