Spring事务管理

一 Spring的事务概念

1 事务概念
(1)什么是事务
事务是是数据库操作的基本单元,是指对一组的数据的操作,要么都成成功,要么都失败
(2)事务特性

  • 原子性
  • 一致性
  • 隔离性
  • 持久性

(3)不考虑隔离性产生读的问题
(4)解决读的问题

  • 设置隔离级别

2 Spring事务管理两种方式
第一种 编程式事务管理(不用)
第二种 声明式事务管理
(1)基于xml配置文件实现
(2)基于注解实现
3 Spring事务管理的api介绍
接口 PlatformTransactionManager 事务管理器
(1)Spring针对不同的dao层框架,提供接口不同的实现类

  • org.springframework.jdbc.datasource.DataSourceTransactionManager 使用mybatis进行持久化数据使用
  • org.springframework.orm.hibernate5.HibernateTransactionManager 使用Hibernate5.0版本进行持久化数据使用

(2)首先配置事务管理器

二 转账环境搭建
1 正确执行

applicationContext.xml



    
    
    
    
        
        
        
        
        
    
 
    
        
        
    
    
    
        
        
    
    
    
        
        
    

OrdersDao.java

package Tx;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created by yang on 17-11-2.
 */
public class OrdersDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    //减少钱
    public void lessMoney(){
        String sql = "update user set salary = salary - ? where id =?";
        jdbcTemplate.update(sql,100,1);
    }
    //增加钱
    public void AddMoney(){
        String sql = "update user set salary = salary + ? where id =?";
        jdbcTemplate.update(sql,100,2);
    }
}

OrdersService.java

package Tx;

/**
 * Created by yang on 17-11-2.
 */
public class OrdersService {
    private OrdersDao ordersDao;

    public void setOrdersDao(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }
    public void money(){
        ordersDao.lessMoney();
        ordersDao.AddMoney();
    }
}

Text.java

package Tx;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by yang on 17-11-2.
 */
public class Text {
    @Test
    public void text(){
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
         OrdersService ordersService = (OrdersService) context.getBean("orderService");
         ordersService.money();
    }
}

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/text?useUnicode=true&characterEncoding=utf-8
执行结果
2 产生问题

如果杨减少100之后,出现异常,邹不会多100,钱丢了。


Spring事务管理_第1张图片
出现异常

执行结果
3 解决

添加事务解决,出现异常进行回滚操作

三 声明式事务管理(xml配置)

第一步 引入约束


第二步 配置事务管理

    
    
        
        
    

第三步 配置事务增强

    
    
        
        
            
            
        
    

第四步 配置切面

    
    
        
        
        
        
    

完整Tx.xml





    
    
    
    
        
        
        
        
        
    

    
    
        
        
    

    
    
        
        
            
            
        
    

    
    
        
        
        
        
    

    
    
        
        
    
    
    
        
        
    
    
    
        
        
    

Spring事务管理_第2张图片
执行结果
  • 增加钱出现异常,减少钱就不会执行
四 声明式事务管理(注解)

第一步 配置事务管理器

    
    
        
    

第二步 开启事务注解

    
    

第三步 在要使用事务的方法所在类上面添加注解@Transactional

package TxAnno;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class OrdersService {
    private OrdersDao ordersDao;

    public void setOrdersDao(OrdersDao ordersDao) {
        this.ordersDao = ordersDao;
    }

    public void money(){
        ordersDao.lessMoney();

        int a = 100/0;

        ordersDao.AddMoney();
    }
}
Spring事务管理_第3张图片
执行结果

你可能感兴趣的:(Spring事务管理)