zoukankan      html  css  js  c++  java
  • Spring 事务传播行为实验

    一、Propagation :

    key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:

    PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
    PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
    PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
    PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
    PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
    PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。

    PROPAGATION_NESTED -- 而Nested的事务和他的父事务是相依的,他的提交是要等和他的父事务一块提交的。也就是说,如果父事务最后回滚,他也要回滚的。

     

     

    二,代码清单

    Mysql 表,

    CREATE TABLE `account` (
       `id` int(11) NOT NULL AUTO_INCREMENT,
       `name` varchar(20) NOT NULL,
       `money` double DEFAULT NULL,
       PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

    初始数据:

    aaa 1000

    bbb 1000

    ccc 1000

     

    public interface InnerService {
    void InnerTransfer(String from, String to, double money);
    }

    @Service
    public class InnerServiceImpl implements InnerService {

    @Autowired
    MyDao myDao;

    @Transactional(propagation = Propagation.MANDATORY)
    public void InnerTransfer(final String from, final String to, final double money) {
    myDao.In(to, money);
    // int a = 1 / 0;
    myDao.Out(from, money);
    }
    }
     
    public interface OuterService {
    void OuterTransfer();
    }
     
    @Service
    public class OuterServiceImpl implements OuterService {


    @Autowired
    InnerService innerService;

    @Autowired
    MyDao myDao;


    public void OuterTransfer() {

    myDao.In("ccc", 19);

    innerService.InnerTransfer("aaa", "bbb", 100d);

    int a = 1 / 0;
    }
    }
     
     
    public interface MyDao {

    void In(String user, double money);

    void Out(String user, double money);
    }
     
     
    @Repository
    public class MyDaoImpl extends JdbcDaoSupport implements MyDao {
    @Autowired
    private DataSource dataSource;

    @PostConstruct
    private void initialize() {
    setDataSource(dataSource);
    }

    public void In(String user, double money) {
    String sql = "update account set money = money + " + money + " where name='" + user + "'";

    this.getJdbcTemplate().update(sql);
    }

    public void Out(String user, double money) {
    String sql = "update account set money = money - ? where name=?";

    this.getJdbcTemplate().update(sql, money, user);
    }

    }
     
     
    配置文件略。
     
    三、事务
     
           
    OuterTransfer InnerTransfer 事务结果 aaa bbb ccc
      MANDATORY        
      NOT_SUPPORTED        
      SUPPORTS        
      REQUIRES_NEW        
      REQUIRED        
      NEVER        
      NESTED        
    MANDATORY          
    MANDATORY MANDATORY        
    MANDATORY NOT_SUPPORTED        
    MANDATORY SUPPORTS        
    MANDATORY REQUIRES_NEW        
    MANDATORY REQUIRED        
    MANDATORY NEVER        
    MANDATORY NESTED        
    NOT_SUPPORTED          
    NOT_SUPPORTED MANDATORY        
    NOT_SUPPORTED NOT_SUPPORTED        
    NOT_SUPPORTED SUPPORTS        
    NOT_SUPPORTED REQUIRES_NEW        
    NOT_SUPPORTED REQUIRED        
    NOT_SUPPORTED NEVER        
    NOT_SUPPORTED NESTED        
    SUPPORTS          
    SUPPORTS MANDATORY        
    SUPPORTS NOT_SUPPORTED        
    SUPPORTS SUPPORTS        
    SUPPORTS REQUIRES_NEW        
    SUPPORTS REQUIRED        
    SUPPORTS NEVER        
    SUPPORTS NESTED        
    REQUIRES_NEW          
    REQUIRES_NEW MANDATORY        
    REQUIRES_NEW NOT_SUPPORTED        
    REQUIRES_NEW SUPPORTS        
    REQUIRES_NEW REQUIRES_NEW        
    REQUIRES_NEW REQUIRED        
    REQUIRES_NEW NEVER        
    REQUIRES_NEW NESTED        
    REQUIRED          
    REQUIRED MANDATORY        
    REQUIRED NOT_SUPPORTED        
    REQUIRED SUPPORTS        
    REQUIRED REQUIRES_NEW        
    REQUIRED REQUIRED        
    REQUIRED NEVER        
    REQUIRED NESTED        
    NEVER          
    NEVER MANDATORY        
    NEVER NOT_SUPPORTED        
    NEVER SUPPORTS        
    NEVER REQUIRES_NEW        
    NEVER REQUIRED        
    NEVER NEVER        
    NEVER NESTED        
    NESTED          
    NESTED MANDATORY        
    NESTED NOT_SUPPORTED        
    NESTED SUPPORTS        
    NESTED REQUIRES_NEW        
    NESTED REQUIRED        
    NESTED NEVER        
    NESTED NESTED        
     
  • 相关阅读:
    Exsi上Windows主机增加硬盘容量
    第6章:vuerouter,vuecli和单文件组件
    Python之爬取天气预报并生成图表
    第3章:vue生命周期及实例的属性
    Python 获得NOAA全球开放气象数据
    第5章:组件即组件间的通信
    MACBOOK M1 PRO 下运行.NET CORE(MAC下如何与X86_64兼容)
    DOTNET 运行AESGCM程序 ON MACOS(错误ALGORITHM ‘AESGCM’ IS NOT SUPPORTED ON THIS PLATFORM)
    1.3\~1.4 控制措施类型、安全框架
    mac 安装brew带来的种种问题
  • 原文地址:https://www.cnblogs.com/zbw911/p/6549089.html
Copyright © 2011-2022 走看看