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支持更丰富的数据结 ...
随机推荐
- Jmeter xpath处理器
- HZOJ 数颜色
一眼看去树套树啊,我可能是数据结构学傻了…… 是应该去学一下莫队进阶的东西了. 上面那个东西我没有打,所以这里没有代码,而且应该也不难理解吧. 这么多平衡树就算了,不过线段树还是挺好打的. 正解3: ...
- 模板—中国剩余定理+拓展GCD
int exgcd(int a,int b,int &x,int &y) { ) { x=,y=; return a; } int gcd=exgcd(b,a%b,x,y); int ...
- console.log详细介绍
console.log详细介绍 效果图: 代码如下: console.log("%c hello world!:http://www.baidu.com","color: ...
- 谷歌BERT预训练源码解析(二):模型构建
目录前言源码解析模型配置参数BertModelword embeddingembedding_postprocessorTransformerself_attention模型应用前言BERT的模型主要 ...
- 如何编程实现快速获取一个整型数中的bit流中1的个数
int one_in_unsigned(unsigned n) { n =(n & ) & 0x55555555); n =(n & ) & 0x33333333); ...
- H3C 分组交换连接模型
- xml path 列转行实例
SQL Server2005提供了一个新查询语法——For XML PATH(''),这个语法有什么用呢?想象一下这样一个查询需求:有两个表,班级表A.学生表B,要查询一个班级里有哪些学生?针对这个需 ...
- supersockets和 AppSession,AppServer 配合工作
现在, 你已经有了 RequestInfo, ReceiveFilter 和 ReceiveFilterFactory, 但是你还没有正式使用它们. 如果你想让他们在你的程序里面可用, 你需要定义你们 ...
- rsa加密(非对称加密)
rsa加密 是非对称加密 需要公钥 与 私钥 这个公钥私钥的具体值需要与后端协商定下 rsa js代码如下 代码太多不插入了 html代码如下 <!DOCTYPE html> <ht ...