目录
- 1.基础
- 2.IOC基于xml的配置
- 3.IOC基于注解的配置
- 4.AOP的相关概念
- 6.spring进阶
- 7.消息监听机制
- 8.事物控制与传播行为
- 9.Spring源码
- 10.spring的生命周期
2.spring对事务的支持
spring中的事务控制是对其他持久层框架事务的支持,spring提供了PlatformTransactionManager接口,其他持久层框架只需要实现该接口,即可已完成对事物的管理
PlatformTransactionManager接口
TransactionStatus getTransaction(TransactionDefinition var1) throws TransactionException;
void commit(TransactionStatus var1) throws TransactionException;
void rollback(TransactionStatus var1) throws TransactionException;
Jdbc提供的持久层接口的实现类为org.springframework.jdbc.datasource.DataSourceTransactionManager
在上面 PlatformTransactionManager 接口有一个方法getTransaction(),这个方法返回的是 TransactionStatus对象,然后程序根据返回的对象来获取事务状态,然后进行相应的操作。
而TransactionStatus这个接口的内容如下:
boolean isNewTransaction();
boolean hasSavepoint();
void setRollbackOnly();
boolean isRollbackOnly();
void flush();
boolean isCompleted();
3.spring进行事务操作的方式
spring对事务的操作方式有
(一)基于编程式的事务管理:很少使用
(二)基于申明式的事务管理:主要采用方式,分为两大方式
--- 采用XML方式
--- 采用注解的方式
(一)基于xml方式的事务
基于xml的事务主要采用了AOP对方法进行增强,达到对事物支持的效果
1.1 创建spring配置文件,并导入约束
此处需要导入 aop 和 tx 两个名称空间
<?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.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.xsd">
</beans>
1.2 编写持久层与业务层代码
1.账户实体类
Account
2.业务层接口
public interface IAccountService {
void transfer(String sourceName,String targeName,Float money);
}
3.业务层接口实现类
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
//转帐事务
@Override
public void transfer(String sourceName, String targeName, Float money) {
accountDao.updateAccount(source);
int i=1/0;
accountDao.updateAccount(target);
}
}
4.持久层接口
public interface IAccountDao {
void updateAccount(Account account);
}
5.持久层接口实现类
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
@Override
public void updateAccount(Account account) {
getJdbcTemplate().update("update account set money = ? where id =?",account.getMoney(),account.getId());
}
}
1.3 在xml中配置持久层与业务层
<!-- 配置 service -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置 dao -->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<!-- 注入 dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置数据源 -->
<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring_day04"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>
1.4对事务管理器的配置
<!-- 配置一个事务管理器 -->
<bean
id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入 DataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
1.5配置 事务的属性
在tx:attributes标签内部 配置事务的属性
<!--
name 指定方法名称:是业务核心方法
read-only:是否是只读事务。默认 false,不只读。
isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。
propagation:指定事务的传播行为。
timeout:指定超时时间。默认值为:-1。永不超时。
rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。
没有默认值,任何异常都回滚。
no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回
滚。没有默认值,任何异常都回滚。
-->
<!-- 配置事务的增强,指定对哪个事务管理器进行增强 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
表示来配置你要增强的方法的匹配的一个规则,
注意:只须改方法的命名规则,其他都是固定的!
propagation:事务的传播行为。
-->
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
1.6 配置AOP切入点表达式
<!-- 配置 aop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut expression="execution(*com.itheima.service.impl.*.*(..))" id="pt1"/>
<!-- 在 aop:config 标签内部:建立事务的通知和切入点表达式的关系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
(二)基于注解的方式
需要在配置文件中开始对事务的支持
<!-- 配置 spring 创建容器时要扫描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启 spring 对注解事务的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
使用@Transcational注解
该注解的属性和 xml 中的属性含义一致。该注解可以出现在接口上,类上和方法上。
- 出现接口上,表示该接口的所有实现类都有事务支持。
- 出现在类上,表示类中所有方法有事务支持
- 出现在方法上,表示方法有事务支持。 以上三个位置的优先级:方法>类>接口
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
public class AccountServiceImpl implements IAccountService {}
4.spring 配置事务回滚点
@Transactional
@RequestMapping("/show")
public String show(){
userService.addUser();
Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
userService.addUser();
TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
return null;
}
使用 Object savePoint =TransactionAspectSupport.currentTransactionStatus().createSavepoint(); 设置回滚点,使用TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);回滚到savePoint。
1.spring事务的传播行为
spring事务的传播行为:当一个方法拥有事务的申明时,其他方法引用该方法,多个事务之间的关系
事务的传播行为有七种
| 事务的传播行为 | 作用 |
|---|---|
| PROPAGATION_REQUIRED | 支持当前事务,如果当前没有事务,就创建一个新事务。如果当前存在事务,就加入该事务,该设置是最常用的设置 |
| PROPAGATION_SUPPORTS | 支持当前事务,如果当前没有事务,就以非事务执行。如果当前存在事务,就加入该事务 |
| PROPAGATION_MANDATORY | 支持当前事务,如果当前不存在事务,就抛出异常。如果当前存在事务,就加入该事务 |
| PROPAGATION_REQUIRES_NEW | 创建新事务,无论当前存不存在事务,都创建新事务 |
| PROPAGATION_NOT_SUPPORTED | 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起 |
| PROPAGATION_NEVER | 以非事务方式执行,如果当前存在事务,则抛出异常 |
| PROPAGATION_NESTED | 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则按REQUIRED属性执行 |
(一)PROPAGATION_REQUIRED
(一)当外部方法没有事务。内部方法创建自己的事务,且相互不干扰
(二)当外部方法创建事务。内部方法加入到外部方法的事务中,一个方法执行失败,所有方法一起失败,即使调用catch方法处理异常,也会失败
user1() {
@Transactional(propagation = Propagation.REQUIRED)
method1() {}
}
user2(){
@Transactional(propagation = Propagation.REQUIRED)
method1(){}
@Transactional(propagation = Propagation.REQUIRED)
method2(){
throw new RuntimeException();
}
}
//验证1,test1()未开启事务,user1.method1(),user2.method1()都执行成功
test1() {
user1.method1();
user2.method1();
throw new RuntimeException();
}
//验证2,test2()未开启事务,user1.method1()执行成功,user2.method2()执行失败
test2(){
user1.method1();
user2.method2();
}
//验证3,test3开启事务,user1.method1(),user2.method1()均执行失败
@Transactional(propagation = Propagation.REQUIRED)
test3() {
user1.method1();
user2.method1();
throw new RuntimeException();
}
//验证4,test4开启事务,user1.method1(),user2.method2()均执行失败
@Transactional(propagation = Propagation.REQUIRED)
test3() {
user1.method1();
user2.method2();
}
//验证5,test5开启事务,user1.method1(),user2.method2()均执行失败
@Transactional(propagation = Propagation.REQUIRED)
test3() {
user1.method1();
try{
user2.method2();
}catch (Exception e){
//do something
}
}
(二)PROPAGATION_REQUIRES_NEW
在外围方法开启事务的情况下Propagation.REQUIRES_NEW修饰的内部方法依然会单独开启独立事务,且与外部方法事务也独立,内部方法之间、内部方法和外部方法事务均相互独立,互不干扰。
user1() {
@Transactional(propagation = Propagation.PROPAGATION_REQUIRES_NEW)
method1() {}
@Transactional(propagation = Propagation.PROPAGATION_REQUIRES)
method2() {}
}
user2(){
@Transactional(propagation = Propagation.PROPAGATION_REQUIRES_NEW)
method1(){}
@Transactional(propagation = Propagation.PROPAGATION_REQUIRES_NEW)
method2(){
throw new RuntimeException();
}
}
//验证1,test1()未开启事务,user1.method1(),user2.method1()都执行成功
test1() {
user1.method1();
user2.method1();
throw new RuntimeException();
}
//验证2,test2()未开启事务,user1.method1()执行成功,user2.method2()执行失败
test2() {
user1.method1();
user2.method2();
}
//验证3,test3开启事务,user1.method1()执行成功,user1.method2()执行失败,user2.method1()执行成功
@Transactional(propagation = Propagation.REQUIRED)
test3() {
user1.method1();
user1.method2();
user2.method1();
throw new RuntimeException();
}
//验证4,test4开启事务,user1.method1()执行成功,user1.method2()执行失败;user2.method1()执行成功,user2.method2()执行失败
@Transactional(propagation = Propagation.REQUIRED)
test4() {
user1.method1();
user1.method2();
user2.method1();
user2.method2();
}
//验证5,test5开启事务,user1.method1()执行成功,user2.method2()均执行失败
@Transactional(propagation = Propagation.REQUIRED)
test3() {
user1.method1();
try{
user2.method2();
}catch (Exception e){
//do something
}
}
(三)PROPAGATION_NESTED
(一)在外部方法没有开启事务时,内部方法单独开启事务,与REQUIRED相同
(二)在外部方法开启事务时,内部方法会加入到外部方法,且抛出的异常可以被catch
user1() {
@Transactional(propagation = Propagation.PROPAGATION_NESTED)
method1() {}
}
user2(){
@Transactional(propagation = Propagation.PROPAGATION_NESTED)
method1(){}
@Transactional(propagation = Propagation.PROPAGATION_NESTED)
method2(){
throw new RuntimeException();
}
}
//验证1,test1()未开启事务,user1.method1(),user2.method1()都执行成功
test1() {
user1.method1();
user2.method1();
throw new RuntimeException();
}
//验证2,test2()未开启事务,user1.method1()执行成功,user2.method2()执行失败
test2(){
user1.method1();
user2.method2();
}
//验证3,test3开启事务,user1.method1(),user2.method1()均执行失败
@Transactional
test3() {
user1.method1();
user2.method1();
throw new RuntimeException();
}
//验证4,test4开启事务,user1.method1(),user2.method2()均执行失败
@Transactional
test3() {
user1.method1();
user2.method2();
}
//验证5,test5开启事务,user1.method1()执行成功,user2.method2()执行失败
@Transactional(propagation = Propagation.REQUIRED)
test3() {
user1.method1();
try{
user2.method2();
}catch (Exception e){
//do something
}
}
三者之间的区别: (一)REQUIRED,REQUIRES_NEW:REQUIRED内外方法的事务是一体的,REQUIRES_NEW内外方法的事务是独立的,只有内外方法的事务类型相同,才会加入到外部事务
(二)REQUIRED,NESTED:内部方法的事务都会加入到外部方法的事务,REQUIRED内部方法抛出的异常捕获后依旧回滚,NESTED抛出的一场捕获后不印象其他事务
(三)REQUIRES_NEW,NESTED:REQUIRES_NEW内外部事务是独立的。NESTED的内部事务依赖外部事务
Spring事务是对数据库事务的支持,Spring支持两种事务的管理方式
(一)基于编程的事务管理类型,通过编程的方式管理事务
(二)给予申明的事务管理类型,这意味着你可以将业务代码和事务管理分离,你只需用注解和XML配置来管理事务。