一、什么是事务?

事务,通俗的说就是,同时做多个事,要么全做,要么不做,也是其特性。举个例子来说,好比你在某宝、某东、某多上购物,在你提交订单的时候,库存也会相应减少,不可能是钱付了,库存不减少,或者库存减少了,钱没扣,不是尴尬了。

二、事务场景实例

没描述清楚?那好,我们结合实例,通过代码实现,我想往数据库加两个学生,如果增加一个失败了,便不再增加,要么就都增加。

ps:此处沿用上篇文章的代码,还请各位同学注意。

1、创建一个service

创建一个名为StudentService的类,用来添加两个学生,示例代码如下:

package com.rongrong.springboot.demo.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @author rongrong
* @version 1.0
* @description:
* @date 2020/1/1 22:21
*/
@Service
public class StudentService { @Autowired
StudentResponstory studentResponstory; public void insertTwoStudent(){
Student student1 = new Student();
student1.setName("Amily");
student1.setAge(17);
student1.setSex("girl");
student1.setEmail("Amily@qq.com");
studentResponstory.save(student1);
Student student2 = new Student();
student2.setName("Jone");
student2.setAge(19);
student2.setSex("boy");
student2.setEmail("Jone@qq.com");
studentResponstory.save(student2);
} }

2、编写接口服务

接口实现增加两个学生,示例代码如下:

    @Autowired
StudentService studentService; /**
* 插入两个学生信息
*/
@PostMapping("/student/insertTwo")
public void insertTwo() {
studentService.insertTwoStudent();
}

启动服务,用postman,调用接口服务,去数据库查看新增学生信息存在,证明接口实现成功,如下图:

三、模拟添加数据失败

接着我们来模拟,数据添加失败情况,修改新增两个学生的方法,将student2的名字改成500字符,代码示例如下:

package com.rongrong.springboot.demo.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @author rongrong
* @version 1.0
* @description:
* @date 2020/1/1 22:21
*/
@Service
public class StudentService { @Autowired
StudentResponstory studentResponstory; public void insertTwoStudent(){
Student student1 = new Student();
student1.setName("Amily");
student1.setAge(17);
student1.setSex("girl");
student1.setEmail("Amily@qq.com");
studentResponstory.save(student1);
Student student2 = new Student();
student2.setName("JoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJone");
student2.setAge(19);
student2.setSex("boy");
student2.setEmail("Jone@qq.com");
studentResponstory.save(student2);
} }

启动项目,再次调用接口,这次真的报错了,先看控制台结果如下图:

因为name字段传入值太长,导致报错,再来看我们的数据库,是否插入数据了呢,

student1成功插入数据了,但是student2并没有,这好比说,你把东西拿到手了,卖方没收到钱,或者你付了钱,没收到货,显然两种情况在现实中都是不允许的,那么我们该怎么解决这样的情况呢

我们请出事务大神。来搞定这个事,具体修改代码示例如下:

package com.rongrong.springboot.demo.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; /**
* @author rongrong
* @version 1.0
* @description:
* @date 2020/1/1 22:21
*/
@Service
public class StudentService { @Autowired
StudentResponstory studentResponstory; @Transactional
public void insertTwoStudent(){
Student student1 = new Student();
student1.setName("Amily");
student1.setAge(17);
student1.setSex("girl");
student1.setEmail("Amily@qq.com");
studentResponstory.save(student1);
Student student2 = new Student();
student2.setName("JoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJoneJone");
student2.setAge(19);
student2.setSex("boy");
student2.setEmail("Jone@qq.com");
studentResponstory.save(student2);
} }

再次启动项目,调用接口服务,我们再看数据库,会看到没数据插入,控制台还会报刚才的错,这就很好的实现了事务的特性,要么做,要么不做

到此,关于事务的介绍和使用介绍完毕,有兴趣的同学可以自行尝试!

spring boot 中事物的使用的更多相关文章

  1. springboot(十一):Spring boot中mongodb的使用

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  2. (转)Spring Boot(十一):Spring Boot 中 MongoDB 的使用

    http://www.ityouknow.com/springboot/2017/05/08/spring-boot-mongodb.html MongoDB 是最早热门非关系数据库的之一,使用也比较 ...

  3. Spring Boot(十一):Spring Boot 中 MongoDB 的使用

    MongoDB 是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配 ...

  4. SpringBoot(十一):Spring boot 中 mongodb 的使用

    原文出处: 纯洁的微笑 mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 ...

  5. springboot:Spring boot中mongodb的使用(山东数漫江湖)

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  6. spring boot @Transactional事物处理

    spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransactionManagement注解 使用时直接在类或者方法上使用 @Tra ...

  7. 如何在Spring Boot 中动态设定与执行定时任务

    本篇文章的目的是记录并实现在Spring Boot中,动态设定与执行定时任务. 我的开发项目是 Maven 项目,所以首先需要在 pom.xml 文件中加入相关的依赖.依赖代码如下所示: <de ...

  8. Spring Boot中的事务是如何实现的

    本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天呢!灯塔君跟大家讲: Spring Boot中的事务是如何实现的 1. 概述 一直在用SpringBoot中的@Transactional来做事 ...

  9. spring boot(三):Spring Boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

随机推荐

  1. 瑞星推国内唯一Linux系统杀毒软件 国产操作系统还需国产安全软件保护

    近来在IT领域最爆炸的新闻莫过于5月20日中央国家机关政府採购中心下发通知.要求中央机关採购所有计算机类产品不同意安装Windows 8.而改用国产Linux操作系统. 此消息一出,立马引起各界关注. ...

  2. 流程控制 Day06

    package com.sxt.arraytest2; public class breakTest { public static void main(String[] args) { label: ...

  3. 04Redis入门指南笔记(内部编码规则简介)

    Redis是一个基于内存的数据库,所有的数据都存储在内存中.所以如何优化存储,减少内存空间占用是一个非常重要的话题.精简键名和键值是最直观的减少内存占用的方式,如将键名very.important.p ...

  4. url地址栏参数<==>对象(将对象转换成地址栏的参数以及将地址栏的参数转换为对象)的实用函数

    /** * @author web得胜 * @param {Object} obj 需要拼接的参数对象 * @return {String} * */ function obj2qs(obj) { i ...

  5. oracle连接多个扫描

    如果你对一个列和一组有限的值进行比较, 优化器可能执行多次扫描并对结果进行合并连接. 举例: SELECT * FROM LODGING WHERE MANAGER IN (‘BILL GATES’, ...

  6. 54个提高PHP程序运行效率的方法

    1.在可以用file_get_contents替代file.fopen.feof.fgets等系列方法的情况下,尽量用 file_get_contents,因为他的效率高得多!但是要注意file_ge ...

  7. JPA多对一单向关联

    在实际开发过程中,JPA多对一单向关联是使用最多的关联方式. 下面是订单与订单项的配置关系. 订单(Order):一的一方,不进行任何配置 @Entity @Table(name="orde ...

  8. p2p平台详细运营框架

    市场拓展部1.负责完成公司市场销售.市场拓展.费用控制等年度目标任务,并负责将目标责任制分解落实,确保各项工作目标得以实现.2.对营销政策.市场及同业营销动态等方面进行调研分析,及时调整营销策略和计划 ...

  9. [转载]Eclipse luna tomcat 控制台 中文乱码

    http://hahalzb.iteye.com/blog/709109 今天做S2SH集成的例子,所有该设置的地方都设置成了UTF-8,包括tomcat的配置文件server.xml.web.xml ...

  10. 2019-8-31-ASP.NET-Core-开启后台任务

    title author date CreateTime categories ASP.NET Core 开启后台任务 lindexi 2019-08-31 16:55:58 +0800 2019-3 ...