spring boot 中事物的使用
一、什么是事务?
事务,通俗的说就是,同时做多个事,要么全做,要么不做,也是其特性。举个例子来说,好比你在某宝、某东、某多上购物,在你提交订单的时候,库存也会相应减少,不可能是钱付了,库存不减少,或者库存减少了,钱没扣,不是尴尬了。
二、事务场景实例
没描述清楚?那好,我们结合实例,通过代码实现,我想往数据库加两个学生,如果增加一个失败了,便不再增加,要么就都增加。
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 中事物的使用的更多相关文章
- springboot(十一):Spring boot中mongodb的使用
mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...
- (转)Spring Boot(十一):Spring Boot 中 MongoDB 的使用
http://www.ityouknow.com/springboot/2017/05/08/spring-boot-mongodb.html MongoDB 是最早热门非关系数据库的之一,使用也比较 ...
- Spring Boot(十一):Spring Boot 中 MongoDB 的使用
MongoDB 是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配 ...
- SpringBoot(十一):Spring boot 中 mongodb 的使用
原文出处: 纯洁的微笑 mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 ...
- springboot:Spring boot中mongodb的使用(山东数漫江湖)
mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...
- spring boot @Transactional事物处理
spring boot 添加事物使用 @Transactional注解 简单使用 在启动类上方添加 @EnableTransactionManagement注解 使用时直接在类或者方法上使用 @Tra ...
- 如何在Spring Boot 中动态设定与执行定时任务
本篇文章的目的是记录并实现在Spring Boot中,动态设定与执行定时任务. 我的开发项目是 Maven 项目,所以首先需要在 pom.xml 文件中加入相关的依赖.依赖代码如下所示: <de ...
- Spring Boot中的事务是如何实现的
本文首发于微信公众号[猿灯塔],转载引用请说明出处 今天呢!灯塔君跟大家讲: Spring Boot中的事务是如何实现的 1. 概述 一直在用SpringBoot中的@Transactional来做事 ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
随机推荐
- 关于使用JavaMail发送邮件抛出java.lang.NoSuchMethodError: com.sun.mail.util.TraceInputStream.<init>(Ljava异常的解决方法
我们在使用JavaMail时有可能会如下异常: Exception in thread "main" java.lang.NoSuchMethodError: com.sun.ma ...
- PyTorch入门学习(二):Autogard之自动求梯度
autograd包是PyTorch中神经网络的核心部分,简单学习一下. autograd提供了所有张量操作的自动求微分功能. 它的灵活性体现在可以通过代码的运行来决定反向传播的过程, 这样就使得每一次 ...
- Part10-字符型设备驱动模型-part10.1-使用字符型设备
‘ ’
- @codechef - SERSUM@ Series Sum
目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @accepted code@ @details@ @description@ ...
- poj 3862 && LA 4589 Asteroids (三维凸包+多面体重心)
3862 -- Asteroids ACM-ICPC Live Archive 用给出的点求出凸包的重心,并求出重心到多边形表面的最近距离. 代码如下: #include <cstdio> ...
- 【DCN】路由操作
offset */interface in/out access-list/prefix-list <1-16> // 修改路由偏移量 RIP偏移列表 ...
- Scoop 包管理工具 安装
本人浏览器已经跨域 https://get.scoop.sh 访问该地址成功 安装前提(所需要的环境): 操作环境:win10. 确保你的 PowerShell 版本 >= 3. win7或许低 ...
- Python __call__详解
可以调用的对象 关于 __call__ 方法,不得不先提到一个概念,就是可调用对象(callable),我们平时自定义的函数.内置函数和类都属于可调用对象,但凡是可以把一对括号()应用到某个对象身上都 ...
- 精选Pycharm里6大神器插件
http://www.sohu.com/a/306693644_752099 上次写了一篇关于Sublime的精品插件推荐,有小伙伴提议再来一篇Pycharm的主题.相比Sublime,Pycharm ...
- Netty进行文件传输
本次是利用TCP在客户端发送文件流,服务端就接收流,写入相应的文件. 实验的源文件是一个图片,假设地址是D:\\Koala.jpg,接收保存后的图片为D:\\test.jpg 原理就是将文件读取成by ...