一、什么是事务?

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

二、事务场景实例

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

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. 洛谷P2330 [SCOI2005]繁忙的都市

    #include<bits/stdc++.h> using namespace std; ; ; int n,k,Max,tot; struct node{ int cnt,fa; }f[ ...

  2. php开发微信支付获取用户地址

    http://mp.weixin.qq.com/s/uNpWE_Z5RZ48PDIWkmGBYQ 使用微信获取地址信息是和微信支付一道申请的,微信支付申请通过,就可以使用该功能. 微信商城中,使用微信 ...

  3. <%@ include file=""%>与<jsp:include page=""/>两种方式的作用以及传值

      一:使用    1.include指令: 1 <%@include file="文件的绝对路径或者相对路径"%> file属性是必填的(绝对或相对路径),但它不支持 ...

  4. 2018年NOIP普及组复赛题解

    题目涉及算法: 标题统计:字符串入门题: 龙虎斗:数学题: 摆渡车:动态规划: 对称二叉树:搜索. 标题统计 题目链接:https://www.luogu.org/problem/P5015 这道题目 ...

  5. 用户注册页的布局及js逻辑实现(正则,注册按钮)

    文章地址:https://www.cnblogs.com/sandraryan/ 先写一个简单的静态页面,然后对用户输入的内容进行验证,判断输入的值是否符合规则,符合规则进行注册 先上静态页面 < ...

  6. keras 保存模型和加载模型

    import numpy as npnp.random.seed(1337) # for reproducibility from keras.models import Sequentialfrom ...

  7. H3C 总线型以太网拓扑扩展

  8. Codeforces Round #168 (Div. 1 + Div. 2)

    A. Lights Out 模拟. B. Convex Shape 考虑每个黑色格子作为起点,拐弯次数为0的格子构成十字形,拐弯1次的则是从这些格子出发直走达到的点,显然需要遍历到所有黑色黑色格子. ...

  9. win2d 渐变颜色

    本文告诉大家如何在 win2d 使用渐变颜色 线条渐变 在 UWP 的 Win2d 使用渐变颜色需要 CanvasLinearGradientBrush 做颜色,本文告诉大家如何在 win2d 使用 ...

  10. el-tree文本内容过多显示不完全问题(解决)

    布局: <span class="custom-tree-node" slot-scope="{ node, data }"> 外层span 树节点 ...