18_Spring_事务管理注解方式

发布时间 2023-07-30 12:49:24作者: AidenDong

18_Spring_事务管理注解方式

事务的管理应该放在我们的service层进行处理

spring中有两种事务的管理方式

1 编程式事务管理(了解)

2 声明式事务管理(掌握)

基于注解方式实现(掌握)

XML方式实现(了解)

Spring声明式事务的实现方式,底层就是AOP,AOP的底层就是动态代理

Spring事务管理相关的API

事务管理器接口: PlatformTransactionManager  针对不同的框架,提供了不同的实现类

image

注解方式实现事务控制

在applicationContext.xml配置事务相关的配置

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
  3.     xmlns:p="http://www.springframework.org/schema/p"
    
  4.     xmlns:c="http://www.springframework.org/schema/c"
    
  5.     xmlns:util="http://www.springframework.org/schema/util"
    
  6.     xmlns:context="http://www.springframework.org/schema/context"
    
  7.     xmlns:aop="http://www.springframework.org/schema/aop"
    
  8.     xmlns:tx="http://www.springframework.org/schema/tx"
    
  9.    xsi:schemaLocation="
    
  10.    http://www.springframework.org/schema/beans
    
  11.    http://www.springframework.org/schema/beans/spring-beans.xsd
    
  12.    http://www.springframework.org/schema/util
    
  13.    http://www.springframework.org/schema/util/spring-util.xsd
    
  14.    http://www.springframework.org/schema/context
    
  15.    http://www.springframework.org/schema/context/spring-context.xsd
    
  16.    http://www.springframework.org/schema/aop
    
  17.    http://www.springframework.org/schema/aop/spring-aop.xsd
    
  18.    http://www.springframework.org/schema/tx
    
  19.    http://www.springframework.org/schema/tx/spring-tx.xsd
    
  20. ">
  21. <!--spring 注解扫描-->
    
  22. <context:component-scan base-package="com.msb"/>
    
  23. <!--读取jdbc配置文件-->
    
  24. <context:property-placeholder location="classpath:jdbc.properties"/>
    
  25. <!--配置德鲁伊连接池-->
    
  26. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    
  27.     <property name="username" value="${jdbc_username}"></property>
    
  28.     <property name="password" value="${jdbc_password}"></property>
    
  29.     <property name="url" value="${jdbc_url}"></property>
    
  30.     <property name="driverClassName" value="${jdbc_driver}"></property>
    
  31. </bean>
    
  32. <!--配置JDBCTemplate对象,并向里面注入DataSource-->
    
  33. <bean id="jdbcTemplate"
    
    class="org.springframework.jdbc.core.JdbcTemplate">
  34.     <!--通过set方法注入连接池-->
    
  35.     <property name="dataSource" ref="dataSource"></property>
    
  36. </bean>
    
  37. <!--配置一个事务管理器-->
    
  38. <bean id="transactionManager"
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  39.     <!--将数据源注入事务管理器-->
    
  40.     <property name="dataSource"  ref="dataSource"></property>
    
  41. </bean>
    
  42. <!--开启事务注解-->
    
  43. <tx:annotation-driven transaction-manager="transactionManager"/>
    

在Service层中添加事务的注解

  1. package com.msb.service.impl;
  2. import com.msb.dao.AccountDao;
  3. import com.msb.service.AccountService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.transaction.annotation.Transactional;
  7. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  8. */
  9. @Service
  10. //@Transactional //加在类上,代表类中的所有方法都添加了事务控制
  11. public class AccountServiceImpl implements AccountService {
  12. @Autowired
    
  13. private AccountDao accountDao;
    
  14. @Override
    
  15. @Transactional// 放在方法上,就是仅仅对当前方法增加了事务控制
    
  16. public int transMoney(int from, int to, int money) {
    
  17.     int rows=0;
    
  18.     rows+=accountDao.transMoney(from, 0 - money);
    
  19.     int i =1/0;
    
  20.     rows+=accountDao.transMoney(to, money);
    
  21.     return rows;
    
  22. }
    
  23. }

再次测试,就算是service方法运行出现异常,自动会回滚,如果没有,那么自动提交

@Transactional 注解的一些参数和参数的含义

@Transactional(propagation = Propagation.REQUIRED,isolation =
Isolation.READ_UNCOMMITTED,readOnly = true,rollbackFor =
ClassCastException.class,noRollbackFor = NullPointerException.class,timeout =
10)

  1. @Target({ElementType.TYPE, ElementType.METHOD})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Inherited
  4. @Documented
  5. public @interface Transactional {
  6.  @AliasFor("transactionManager")
    
  7.  String value() default "";
    
  8.  @AliasFor("value")
    
  9.  String transactionManager() default "";
    
  10. String[] label() default {};
    
  11. Propagation propagation() default Propagation.REQUIRED;
    
  12. Isolation isolation() default Isolation.DEFAULT;
    
  13. int timeout() default -1;
    
  14. String timeoutString() default "";
    
  15. boolean readOnly() default false;
    
  16. Class<? extends Throwable>[] rollbackFor() default {};
    
  17. String[] rollbackForClassName() default {};
    
  18. Class<? extends Throwable>[] noRollbackFor() default {};
    
  19. String[] noRollbackForClassName() default {};
    
  20. }

propagation  事务的传播行为(面试)

多事务方法之间调用,事务是如何管理的

事务传播行为类型 说明
PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择(默认)。
PROPAGATION_SUPPORTS 支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY 使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW 新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER 以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。

如果service层  add方法调用了 addDept和addEmp两个方法

PROPAGATION_REQUIRED

如果add方法有事务,那么addDept和addEmp就加入到add方法里的事务

如果add方法没有事务,那么就新建一个事务,将addDept和addEmp加入到这个新的事务中

PROPAGATION_REQUIRES_NEW

无论add是否有事务,都建立一个新的事务,所有的方法都加入到新的事务中,add原来的事务就不用了

isolation 事务的隔离级别

  1. DEFAULT (默认) 
    这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别。另外四个与JDBC的隔离级别相对应。
    MySQL默认REPEATABLE_READ

Oracle默认READ_COMMITTED

  1. READ_UNCOMMITTED (读未提交) 
    这是事务最低的隔离级别,它允许另外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。

  2. READ_COMMITTED
    (读已提交) 
    保证一个事务修改的数据提交后才能被另外一个事务读取,另外一个事务不能读取该事务未提交的数据。这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。

  3. REPEATABLE_READ (可重复读) 
    这种事务隔离级别可以防止脏读、不可重复读,但是可能出现幻像读。它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了不可重复读。

SERIALIZABLE(串行化) 
这是花费最高代价但是最可靠的事务隔离级别,事务被处理为顺序执行。除了防止脏读、不可重复读外,还避免了幻像读。

timeout 超时时间

事务一定要在多长时间之内提交,如果不提交就会回滚

readOnly 只读事务

事务是否只能读取数据库的数据,如果为true,则不允许进行增删改

rollbackFor 指定发生回滚的异常

当方法发生哪些异常时才会回滚

noRollbackFor 指定不发生回滚的异常

当方法发生哪些异常时,不会回滚