转载自 https://www.2cto.com/kf/201607/529762.html

一、前言

最近开发程序的时候,出现数据库自增id跳数字情况,无奈之下dba遍查操作日志,没有delete记录。才开始慢慢来查询事物问题。多久以来欠下的账,今天该还给spring事物。 希望大家有所收获。2016年07月19日22:32:38

二、spring嵌套事物

1、展示项目代码--简单测springboot项目

整体项目就这么简单,为了方便。这里就只有biz层与service层,主要作为两层嵌套,大家只要看看大概就ok。后面会给出git项目地址,下载下来看一看就明白,力求最简单。

下面我们分情况介绍异常。

Controller 调用层(没有使用它作为外层,因为controller作为外层要在servlet-mvc.xml 配置就ok。但是我觉得比较麻烦,一般也不推荐)

?
1
package com.ycy.app; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.annotation.ImportResource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by ycy on 16/7/19. */ @RestController @SpringBootApplication @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) @ImportResource({"classpath:/applicationContext.xml"}) public class Application { @Autowired private TestBiz testBiz; @RequestMapping("/") String home() throws Exception { System.out.println("controller 正常执行"); testBiz.insetTes(); return " 正常返回Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
?
1
package com.ycy.app; import com.ycy.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; /** * Created by ycy on 16/7/20. */ @Component public class TestBiz { @Autowired private TestService testService; @Transactional public void insetTes() { for (int j = 0; j < 8; j++) { testService.testInsert(j, j + "姓名"); } System.out.println("biz层 正常执行"); } }

Service层 (内层)

?
1
package com.ycy.service.impl; import com.ycy.center.dao.entity.YcyTable; import com.ycy.center.dao.mapper.YcyTableMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * Created by ycy on 16/7/19. */ @Service public class TestServiceImpl implements com.ycy.service.TestService { @Autowired private YcyTableMapper ycyTableMapper; @Transactional public void testInsert(int num,String name) { YcyTable ycyTable=new YcyTable(); ycyTable.setName(name); ycyTable.setNum(num); ycyTableMapper.insert(ycyTable); System.out.println(num+"service正常执行"); } }
2、外部起事物,内部起事物,内外都无Try Catch

外部异常:

代码展示,修改外层Biz层代码如下

@Component public class TestBiz { @Autowired private TestService testService; @Transactional public void insetTes() { for (int j = 0; j < 8; j++) { testService.testInsert(j, j + "姓名"); if (j == 3) { int i = 1 / 0;// 此处会产生异常 } } System.out.println("biz层 正常执行"); } }

打印执行结果:0-3service正常执行 数据库结果:全部数据回滚

外部异常总结:内外都无try Catch的时候,外部异常,全部回滚。

内部异常:

代码展示,修改service层代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.ycy.service.impl;
import com.ycy.center.dao.entity.YcyTable;
import com.ycy.center.dao.mapper.YcyTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * Created by ycy on 16/7/19.
 */
@Service
public class TestServiceImpl implements com.ycy.service.TestService {
  @Autowired
  private YcyTableMapper ycyTableMapper;
  @Transactional
  public void testInsert(int num, String name) {
    YcyTable ycyTable = new YcyTable();
    ycyTable.setName(name);
    ycyTable.setNum(num);
    ycyTableMapper.insert(ycyTable);
    if (num == 3) {
      int i = 1 / 0;// 此处会产生异常
    }
    System.out.println(num + "service正常执行");
  }
}

打印执行结果:0-3service正常执行 数据库结果:全部数据回滚

内部异常总结:内外都无try Catch的时候,内部异常,全部回滚。

3、外部起事物,内部起事物,外部有Try Catch

外部异常:

代码展示,修改biz层代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component
public class TestBiz {
  @Autowired
  private TestService testService;
  @Transactional
  public void insetTes() {
    try {
      for (int j = 0; j < 8; j++) {
        testService.testInsert(j, j + "姓名");
        if (j == 3) {
          int i = 1 / 0;// 此处会产生异常
        }
      }
    } catch (Exception ex) {
      System.out.println("异常日志处理");
    }
    System.out.println("biz层 正常执行");
  }
}

打印结果:0-3执行正常数据库结果:4条数据

外部异常总结:外部有try Catch时候,外部异常,不能回滚(事物错误)

内部异常:

代码展示,修改service层代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Service
public class TestServiceImpl implements com.ycy.service.TestService {
  @Autowired
  private YcyTableMapper ycyTableMapper;
  @Transactional
  public void testInsert(int num, String name) {
    YcyTable ycyTable = new YcyTable();
    ycyTable.setName(name);
    ycyTable.setNum(num);
    ycyTableMapper.insert(ycyTable);
    if (num == 3) {
      int i = 1 / 0;// 此处会产生异常
    }
    System.out.println(num + "service正常执行");
  }
}

打印结果:0-2打印正常 数据库结果:无数据,全部数据回滚 内部异常总结:外部有try Catch时候,内部异常,全部回滚

4、外部起事物,内部起事物,内部有Try Catch

外部异常:

代码展示,修改biz层:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Component
public class TestBiz {
  @Autowired
  private TestService testService;
  @Transactional
  public void insetTes() {
    for (int j = 0; j < 8; j++) {
      testService.testInsert(j, j + "姓名");
      if (j == 3) {
        int i = 1 / 0;// 此处会产生异常
      }
    }
    System.out.println("biz层 正常执行");
  }
}

打印结果:0-3service打印正常 数据库结果:无数据,全部数据回滚


外部异常总结:内部有try Catch,外部异常,全部回滚

内部异常:

修改service层代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Service
public class TestServiceImpl implements com.ycy.service.TestService {
  @Autowired
  private YcyTableMapper ycyTableMapper;
  @Transactional
  public void testInsert(int num, String name) {
    try {
      YcyTable ycyTable = new YcyTable();
      ycyTable.setName(name);
      ycyTable.setNum(num);
      ycyTableMapper.insert(ycyTable);
      if (num == 3) {
        int i = 1 / 0;// 此处会产生异常
      }
    } catch (Exception ex) {
      System.out.println(num + "service异常日志");
    }
    System.out.println(num + "service正常执行");
  }
}

打印结果:0-0service打印正常 数据库结果:没有回滚

内部异常总结:内部有try Catch,内部异常,全部不回滚(事物失败);

5、外部起事物,内部起事物,内外有Try Catch

外部异常:

代码展示,修改biz层:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component
public class TestBiz {
  @Autowired
  private TestService testService;
  @Transactional
  public void insetTes() {
    try {
      for (int j = 0; j < 8; j++) {
        testService.testInsert(j, j + "姓名");
        if (j == 3) {
          int i = 1 / 0;// 此处会产生异常
        }
      }
    } catch (Exception ex) {
      System.out.println("biz层异常日志处理");
    }
    System.out.println("biz层 正常执行");
  }
}

打印结果:0-3service打印正常 数据库结果:插入三条数据,没有回滚

外部异常总结:内外都有try Catch,外部异常,事物执行一半(事物失败)

内部异常:

代码展示,修改service 层代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Service
public class TestServiceImpl implements com.ycy.service.TestService {
  @Autowired
  private YcyTableMapper ycyTableMapper;
  @Transactional
  public void testInsert(int num, String name) {
    try {
      YcyTable ycyTable = new YcyTable();
      ycyTable.setName(name);
      ycyTable.setNum(num);
      ycyTableMapper.insert(ycyTable);
      if (num == 3) {
        int i = 1 / 0;// 此处会产生异常
      }
    } catch (Exception ex) {
      System.out.println(num + "service异常日志处理");
    }
    System.out.println(num + "service正常执行");
  }
}

打印结果:0-7service打印正常,3异常日子好 数据库结果:插入全部,没有回滚

内部事物总结:内外都有try Catch,内部异常,事物全部不会滚(事物失败)

三、嵌套事物总结

事物成功总结

1、内外都无try Catch的时候,外部异常,全部回滚。

2、内外都无try Catch的时候,内部异常,全部回滚。

3、外部有try Catch时候,内部异常,全部回滚

4、内部有try Catch,外部异常,全部回滚

5、友情提示:外层方法中调取其他接口,或者另外开启线程的操作,一定放到最后!!!(因为调取接口不能回滚,一定要最后来处理)

总结:由于上面的异常被捕获导致,很多事务回滚失败。如果一定要将捕获,请捕获后又抛出RuntimeException(默认为异常捕获RuntimeException)。

四、正确的嵌套事物实例

controller层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.ycy.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by ycy on 16/7/19.
 */
@RestController
@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@ImportResource({"classpath:/applicationContext.xml"})
public class Application {
  @Autowired
  private TestBiz testBiz;
  @RequestMapping("/")
  String home()  {
    System.out.println("controller 正常执行");
    try {
      testBiz.insetTes();
    } catch (Exception e) {
      System.out.println("controller 异常日志执行");
    }
    return " 正常返回Hello World!";
  }
  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }
}

外层biz层:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.ycy.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.ycy.service.TestService;
/**
 * Created by ycy on 16/7/20.
 */
@Component
public class TestBiz {
  @Autowired
  private TestService testService;
  @Transactional
  public void insetTes() throws Exception {
    try {
      for (int j = 0; j < 8; j++) {
        testService.testInsert(j, j + "姓名");
        if (j == 3) {
          int i = 1 / 0;// 此处会产生异常
        }
      }
    } catch (Exception ex) {
      System.out.println("biz层异常日志处理");
      throw new RuntimeException(ex);
    }
    System.out.println("biz层 正常执行");
  }
}

内层service层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.ycy.service.impl;
import com.ycy.center.dao.entity.YcyTable;
import com.ycy.center.dao.mapper.YcyTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * Created by ycy on 16/7/19.
 */
@Service
public class TestServiceImpl implements com.ycy.service.TestService {
  @Autowired
  private YcyTableMapper ycyTableMapper;
  @Transactional
  public void testInsert(int num, String name) throws Exception {
    try {
      YcyTable ycyTable = new YcyTable();
      ycyTable.setName(name);
      ycyTable.setNum(num);
      ycyTableMapper.insert(ycyTable);
      if (num== 3) {
        int i = 1 / 0;// 此处会产生异常
      }
    } catch (Exception ex) {
      System.out.println(num + "service异常日志处理");
        throw new RuntimeException(ex);
    }
    System.out.println(num + "service正常执行");
  }
}

Spring事务管理--嵌套事务详解相关推荐

  1. Spring事务管理嵌套事务详解 : 同一个类中,一个方法调用另外一个有事务的方法

    Spring 事务机制回顾 Spring事务一个被讹传很广说法是:一个事务方法不应该调用另一个事务方法,否则将产生两个事务.  结果造成开发人员在设计事务方法时束手束脚,生怕一不小心就踩到地雷.    ...

  2. Spring事务管理(详解+实例)

    写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: ...

  3. Spring进阶(五):Spring事务管理(详解+实例)

    文章目录 一.前言 二.核心接口 2.1 事务管理器 2.1.1 JDBC事务 2.1.2 Hibernate事务 2.1.3 Java持久化API事务(JPA) 2.1.4 Java原生API事务 ...

  4. 编程式事务管理(详解)

    第一章:编程式事务管理(详解) 1. 说明:Spring为了简化事务管理的代码:提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可!! 2. 手 ...

  5. spring的annotation-driven配置事务管理器详解

    来源:http://blog.sina.com.cn/s/blog_8f61307b0100ynfb.html 这篇文章是我从ITeye上复制来的,看了一遍,觉得很深刻,决定把他复制来,对原作者表示感 ...

  6. Spring事务源码详解

    一. 简介 事务: 事务是逻辑上的一组操作,要么都执行,要么都不执行,关于事务的基本知识可以看我的这篇文章:事务的基础知识 Spring事务: Spring 支持两种方式的事务管理:编程式事务管理.声 ...

  7. Spring事务传播行为详解

    前言 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为.事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为.这是Spring ...

  8. 《Spring事务传播行为详解》经典例子 看完这篇,别的不用看了

    前言 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为.事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为.这是Spring ...

  9. java事物传播行为,Spring事务传播行为详解

    前言 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为.事务传播行为是Spring框架独有的事务增强特性,他不属于的事务实际提供方数据库行为.这是Spring ...

最新文章

  1. Python之列表list转换成字典dict的的方法【使用内置函数zip和dict快速搞定】
  2. OpenCV 直方图的计算和绘制
  3. 华为,为什么让全世界都感到害怕?
  4. 微软算法面试题(2)
  5. php关键词分词搜索 最多匹配的排在最前面_图解 | 通用搜索引擎背后的技术点...
  6. 实验:sigsuspend(),sigprocmask()
  7. 关于html的一些杂技
  8. 开发日记:接口开发设计
  9. 使用Areas分离ASP.NET MVC项目
  10. VC2012/VS2012 MFC串口通讯上位机程序教程笔记
  11. SG90舵机驱动,有代码
  12. 出去锻炼还是不让数据科学决定
  13. 2021年微软研究博士奖研金名单出炉!三位华人博士生入选,每人42000美元
  14. MOOS-ivp 实验十四(2)behavior简要概述
  15. android绘制view的撤销,DrawingView android 上的一个涂鸦控件。可以设置画笔的粗细,颜色,撤销上一笔涂鸦,提供保存图片的接口。 @codeKK Android开源站...
  16. 用计算机找女朋友,大学生必备远程软件:能找女朋友、玩游戏、赚零花钱
  17. 腾讯产培生笔试(内附作者答案)
  18. 基于STM32单片机的密码锁(Proteus仿真+程序+报告)
  19. 耐压测试仪结构组成部分
  20. 【数据结构-ZZU】01. 绪论

热门文章

  1. Two Merged Sequences(CF 1144 G)
  2. Win10任务计划+PowerShell实现Git自动推送
  3. Shell函数:Shell函数返回值、删除函数、在终端调用函数
  4. live555 源码分析:ServerMediaSession
  5. 白话科普系列——网站靠什么提升加载速度?
  6. leetcode-260.只出现一次的数字 III 解法
  7. 第41讲:Scrapy框架的介绍
  8. Redis源码:朴实无华且枯燥
  9. ​Hmily重启后月度报告
  10. 搞定系统设计 03:系统设计面试的答题框架