手撕Spring5框架(十)Spring实现事务的管理

Spring实现事务的管理的步骤

1、事务要添加到三层结构里的Service层(业务逻辑层)

2、Spring进行事务管理操作有两种方式

1)编程式事务管理(不建议使用,该方式是在程序里编写事务相关的代码)

2)声明式事务管理(建议使用,通过配置的方式操作事务)

3、声明式事务管理具体实现有两种方式

1)基于注解的方式(推荐使用)

2)基于xml配置文件的方式

4、在Spring进行声明式事务管理,底层使用的AOP原理

5、Springs事务管理相关API

1)提供一个接口,代表事务管理器,这个接口针对不同的数据持久框架提供不同的实现类

PlatformTransactionManager

手撕Spring5框架(十)Spring实现事务的管理_第1张图片

  • 基于注解声明式事务管理的具体实现

1、在Spring配置文件配置事务管理器



    
    

 

2、在Spring配置文件开启事务注解

1)增加命名空间

2)增加开启事务注解的设置

 
    

3、在Service类或者该类的方法上增加事务注解@Transactional

类上添加,表明事务在该类所有的方法生效

方法上添加,表明事务只在该方法上生效

package org.learn.spring5.service.impl;

import org.learn.spring5.dao.UserDao;
import org.learn.spring5.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    public void transferAccount() {

        //小明转账100,减少100元
        userDao.reduceMoney();
        //小红账户增加100元
        userDao.addMoney();
    }
}

 4.编写测试程序

import org.junit.Test;
import org.learn.spring5.service.UserService;
import org.learn.spring5.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring5 {


    /**
     * 模拟转账操作(事务管理的引入)
     */
    @Test
    public void testAccount() {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
        userService.transferAccount();//转账操作
    }



}

我们在UserServiceImpl类的转账方法里模拟一个异常,让程序出错,看看我们的事务配置的是否生效,正常情况应该是如果程序出现异常,service类对应的方法中执行数据操作的方法将会全部失败,数据不发生变化。

@Transactional注解相关属性的介绍

1)propagation:传播行为,描述由某一个事务方法被嵌套另一个方法时事务如何传播。

传播行为属性值设置有七种如下:

手撕Spring5框架(十)Spring实现事务的管理_第2张图片

 默认是REQUIRED

@Transactional(propagation = Propagation.REQUIRED)

2)isolation:隔离级别,解决隔离性的问题,多事务(并发操作)操作之间不会产生影响

如果不考虑隔离性,会出现很多问题。

例如出现三个读的问题,

1)脏读,一个未提交事务读取到另一个未提交事务的数据。

2)不可重复读,一个未提交的事务读取到另一个已提交事务中修改的数据。

3)虚(幻)读,一个未提交的事务读取到另一个已提交事务中添加的数据。

Spring事务管理中隔离级别的设置就是针对如上问题进行处理的,具体设置的值如下:

手撕Spring5框架(十)Spring实现事务的管理_第3张图片

@Transactional(propagation = Propagation.REQUIRED ,isolation = Isolation.REPEATABLE_READ)

 3)timeout:超时时间,事务要在一个规定时间内提交,如果不提交就会超时,事务就会回滚,默认值为-1,表示不超时。

 4)readOnly:是否只读,默认值false,如果 设置true,只能查询,不能添加、删除、修改操作。

@Transactional(readOnly = false,propagation = Propagation.REQUIRED ,isolation = Isolation.REPEATABLE_READ)

5)rollbackFor:回滚,出现哪些异常进行事务回滚。

6)noRollbackFor:不回滚,出现哪些异常,事务不进行事务回滚。

@Transactional(readOnly = false,propagation = Propagation.REQUIRED ,isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
  • 基于XML配置文件声明式事务管理的具体实现

在Spring的配置文件中进行配置,具体内容如下:

1)配置事务管理

2)配置通知

3)配置切入点和切面




    
    
    

    

    


    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
        
    
    
    
        
        
            
            
        
    

    
    
        

        
        
    

  • 完全注解事务管理实现

添加配置类SpringConfig,删除xml配置文件

package org.learn.spring5.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(basePackages = "org.learn.spring5")  //开启组件扫描
@EnableTransactionManagement  //开启事务
public class SpringConfig {

    //创建数据库连接池
    @Bean
    public DruidDataSource getDruidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/spring5-demo?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=CONVERT_TO_NULL");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        return  druidDataSource;
    }
    //创建JdbcTemplate
    @Bean
    public JdbcTemplate JdbcTemplate(DruidDataSource druidDataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource);
        return  jdbcTemplate;
    }
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DruidDataSource druidDataSource){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(druidDataSource);

        return transactionManager;
    }

}

编写测试程序

    /**
     * 模拟转账操作(完全註解形式)
     */
    @Test
    public void testAccount() {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean("userServiceImpl", UserService.class);
        userService.transferAccount();//转账操作
    }

 

你可能感兴趣的:(手撕Spring5框架,spring,spring,boot,后端)