Spring事务管理--(二)嵌套事物详解

一、前言

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

二、spring嵌套事物

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

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

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

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

<pre name="code" class="html">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);
  }
}

Biz层(外层)

<pre name="code" class="html">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层  (内层)

<pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html">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层代码如下

<pre name="code" class="html"><pre name="code" class="html">@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层代码

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层代码

@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层代码:

@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层:

@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层代码:

@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层:

@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 层代码

@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层

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层:

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层

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正常执行");
  }
}

项目地址:https://github.com/yangchangyong0/springTransactional    项目git地址

时间: 2024-08-27 00:01:54

Spring事务管理--(二)嵌套事物详解的相关文章

JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权

shiro介绍 什么是shiro shiro是Apache的一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架.它可以实现如下的功能: 1.验证用户 2.对用户执行访问控制,如:判断用户是否具有角色admin,判断用户是否拥有访问的资源权限. 3.在任何环境下使用SessionAPI.例如C/S程序 4.可以使用多个用户数据源.例如一个是Oracle数据库,另外一个是MySQL数据库. 5.单点登录(SSO)功能

程序包管理二之yum详解

上节向大家介绍了程序包管理的理论和rpm命令的使用,本篇文章接着向大家介绍rpm的前端使用工具,方便大家更快捷的管理,使用rpm包,提高工作效率. Linux程序包管理(2): Centos:yum,dnf 两个rpm前端工具 URL:ftp://10.1.0.1/pub/ YUM :yellow dog, Yellowdog,Update Modifier 采取cs架构 从文件服务器加载rpm包,安装程序到本地主机 yum repository:yum repo 存储了众多rpm包,以及包的相

[转]Java程序员从笨鸟到菜鸟之(八十三)细谈Spring(十二)OpenSessionInView详解及用法

首先我们来看一下什么是OpenSessionInView?    在hibernate中使用load方法时,并未把数据真正获取时就关闭了session,当我们真正想获取数据时会迫使load加载数据,而此时session已关闭,所以就会出现异常. 比较典型的是在MVC模式中,我们在M层调用持久层获取数据时(持久层用的是load方法加载数据),当这一调用结束时,session随之关闭,而我们希望在V层使用这些数据,这时才会迫使load加载数据,我们就希望这时的session是open着得,这就是所谓

Spring基础学习(二)&mdash;注入参数详解

     在Spring配置文件中,用户不但可以将String.int等字面值注入Bean中,还可以将集合.Map等类型注入Bean中,此外还可以注入配置文件中其他定义的Bean. 一.字面值      (1)可用字符串表示的值,可以通过<value>元素标签或value属性进行注入.      (2)基本数据类型及其封装类.Stting类型都可以采用字面值注入的方式.      (3)若字面值包含特殊字符,可以使用<![CDATA[]]>把字面值包裹起来. <bean id

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

文章转自:http://blog.csdn.net/trigl/article/details/50968079 写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: Spring事务机制详解 Spring事务配置的五种方式 Spring中的事务管理实例详解 一.初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱.  比如你去ATM机取1000块钱,

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

写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: Spring事务机制详解 Spring事务配置的五种方式 Spring中的事务管理实例详解 1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必须是要么都执行要么都

这可能是最漂亮的Spring事务管理详解

事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么完全不起作用: 一致性: 执行事务前后,数据保持一致: 隔离性: 并发访问数据库时,一个用户的事物不被其他事物所干扰,各并发事务之间数据库是独立的: 持久性: 一个事务被提交之后.它对数据库中数据的改变是持久的,即使数据库发生故障也不应该对其有任何影响. Spring事务管理接口介绍 Spring事务管理接口:

一文解析Spring事务管理详解;通俗易懂,轻松掌握!

事务概念回顾 什么是事务? 事务是逻辑上的一组操作,要么都执行,要么都不执行. 事物的特性(ACID): 原子性:?事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么完全不起作用: 一致性:?执行事务前后,数据保持一致: 隔离性:?并发访问数据库时,一个用户的事物不被其他事物所干扰,各并发事务之间数据库是独立的: 持久性:?一个事务被提交之后.它对数据库中数据的改变是持久的,即使数据库发生故障也不应该对其有任何影响. Spring事务管理接口介绍 Spring事务管理接口:

spring 事务管理详解 学习心得

今天,我终于登上了你的诺曼底,spring事务. 在此之前,一谈起spring我就没底,虽然用的很顺手,但是其中的AOP和事务一直未理解和掌握,数次尝试突破都未成功,之前看过很多网上的相关文章和书籍,要么基于的版本不同,有的基于spring2有的基于spring3:要么切入点不同,有的讲的太低级,我都懂,有的讲的太庞杂,我晕了...... 从这周一开始,我决定在试一下.计划每天的上午专门学习,横扫各大网站,收集文章,然后对其分类,整理记笔记,到周二坚持一个一个的看,规整,理解,熟记,本子下写下了