标签:例子 port 字符 pack 创建 style work void src
事务,通俗的说就是,同时做多个事,要么全做,要么不做,也是其特性。举个例子来说,好比你在某宝、某东、某多上购物,在你提交订单的时候,库存也会相应减少,不可能是钱付了,库存不减少,或者库存减少了,钱没扣,不是尴尬了。
没描述清楚?那好,我们结合实例,通过代码实现,我想往数据库加两个学生,如果增加一个失败了,便不再增加,要么就都增加。
ps:此处沿用上篇文章的代码,还请各位同学注意。
创建一个名为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); } }
接口实现增加两个学生,示例代码如下:
@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); } }
再次启动项目,调用接口服务,我们再看数据库,会看到没数据插入,控制台还会报刚才的错,这就很好的实现了事务的特性,要么做,要么不做
到此,关于事务的介绍和使用介绍完毕,有兴趣的同学可以自行尝试!
标签:例子 port 字符 pack 创建 style work void src
原文地址:https://www.cnblogs.com/longronglang/p/12130864.html