博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Javaweb——Spring Boot 系列(13)数据库事务
阅读量:3934 次
发布时间:2019-05-23

本文共 6828 字,大约阅读时间需要 22 分钟。

数据库事务

一、Spring Boot 的事务机制

  • 在之前的博文已经写过 Spring 事务方面的,今天的针对 Spring Boot 的事务机制进行学习。

1、Spring Boot 的事务机制的支持

  • 无论是使用 JDBC 数据访问技术,还是用 JPA 数据访问技术,Spring Boot 都为我们定义了一个 PlatformTransactionManager 的实现专门的 Bean,如 JDBC 的 DataSourceTransactionManager,如 JPA 的 JpaTransactionManager,并且我们进行了自动配置。

二、示例项目

  • 同样用一个简单的项目测试特定错误引起事务回滚和特定错误下事务不回滚。

1、新建项目

  • 用 IDEA 的项目创建向导创建一个带初始依赖的 Spring Boot 项目,初始依赖选择 Spring Web 和 Spring Data JPA。

2、添加依赖

  • 因为目标数据库仍旧是 Oracle 数据库,因此还是需要 ojdbc6.jar 这个包,在 POM 文件添加如下的坐标:
    com.oracle
    ojdbc6
    11.2.0.2.0
  • 项目结构信息如下:
    com.pyc
    transaction
    0.0.1-SNAPSHOT
    transaction
    jar
    Demo project for Spring Boot
  • Spring Boot 的版本为 1.3.0.M2
    org.springframework.boot
    spring-boot-starter-parent
    1.3.0.M2
  • 整个 POM 文件如下:
    4.0.0
    org.springframework.boot
    spring-boot-starter-parent
    1.3.0.M2
    com.pyc
    transaction
    0.0.1-SNAPSHOT
    transaction
    jar
    Demo project for Spring Boot
    UTF-8
    1.8
    com.oracle
    ojdbc6
    11.2.0.2.0
    org.springframework.boot
    spring-boot-starter-data-jpa
    org.springframework.boot
    spring-boot-starter-web
    org.springframework.boot
    spring-boot-starter-test
    test
    org.junit.vintage
    junit-vintage-engine
    org.springframework.boot
    spring-boot-maven-plugin

3、application.properties 配置

  • 在 application.properties 中对数据源和数据驱动等进行配置
    # 数据驱动 database driverspring.datasource.driverClassName=oracle.jdbc.OracleDriver# 数据源 data sourcespring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:xe# 数据库登录用户 database account and also name of the goal databasespring.datasource.username=boot# 相应用户的登录密码 password of the corresponding database accountspring.datasource.password=boot# auto maintain data sheet of goal databasespring.jpa.hibernate.ddl-auto=update# show sql in console windowspring.jpa.show-sql=truespring.jackson.serialization.indent-output=truedebug=truelogging.file=log.loglogging.level.org.springframework.web=DEBUG

4、Entity class

  • 准备一个实体类,当然这里因为目标数据源一样,所以实体类仍旧是 Person,具体代码见上一篇。

5、Entity Repository

  • 准备实体类对应的 Repository,这在 JPA 数据访问技术中是必不可少的,在这个项目中无需自定义任何 Repository 方法,只要继承 JpaRepository 的方法就够了,代码如下:
    //IntelliJ IDEA//transaction//PersonRepository//2020/2/11// Author:御承扬//E-mail:2923616405@qq.compackage com.pyc.transaction.dao;import com.pyc.transaction.domain.Person;import org.springframework.data.jpa.repository.JpaRepository;public interface PersonRepository extends JpaRepository
    {
    }

6、Business Service

  • 要以事务的形式操作数据库,就需要相应的业务服务程序。

6.1、Service Interface

  • 首先是编写一个服务接口,这里主要就两个方法:一个发生特定异常时事务回滚,另一个是发生特定异常时事务不回滚。
    package com.pyc.transaction.service;import com.pyc.transaction.domain.Person;public interface DemoService {
    public Person savePersonWithRollBack(Person person); public Person savePersonWithoutRollBack(Person person);}

6.2、Service Achieve

  • 编写一个继承服务接口的类,并事项接口中的所有方法,代码如下:
    package com.pyc.transaction.service.impl;import com.pyc.transaction.dao.PersonRepository;import com.pyc.transaction.domain.Person;import com.pyc.transaction.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class DemoServiceImpl implements DemoService {
    @Autowired PersonRepository personRepository; // 使用 @Transaction 注解的 rollback 属性,指定特定异常时,数据回滚 // comment @Transaction and use property rollback to assign transaction rollback // when happen illegal argument exception @Transactional(rollbackFor = {
    IllegalArgumentException.class}) public Person savePersonWithRollBack(Person person){
    Person p = personRepository.save(person); if(person.getName().equals("林秋延")){
    throw new IllegalArgumentException("林秋延已经存在,数据回滚"); } return p; } // comment @Transaction and use property rollback to assign transaction no rollback // when happen illegal argument exception @Transactional(noRollbackFor = {
    IllegalArgumentException.class}) public Person savePersonWithoutRollBack(Person person){
    Person p = personRepository.save(person); if(person.getName().equals("pyc")){
    throw new IllegalArgumentException("pyc 虽然已经存在,数据不会回滚"); } return p; }}
  • 在类体上用 @Service 注解,向 Spring 表面这是一个服务类,项目启动时就会自动扫描当前类体。
  • 类体中的两个方法都用代码 throw 一个异常,这种方式叫做硬编码手动触发异常

7、Controller

  • 为了前台能够调用刚刚 Service 中的相关方法,需要用一个 Controller 编辑相应的 URL 暴露到前台中去,代码如下:
    package com.pyc.transaction.web;import com.pyc.transaction.domain.Person;import com.pyc.transaction.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class WebController {
    @Autowired DemoService demoService; // 测试回滚 // try rollback transaction @RequestMapping("/rollback") public Person rollback(Person person){
    return demoService.savePersonWithRollBack(person); } // trying no rollback transaction @RequestMapping("/norollback") public Person noRollback(Person person){
    return demoService.savePersonWithoutRollBack(person); }}
  • 调用特定异常发生时事务回滚的方法的 URL 为 ”/rollback“。
  • 入口类无需修改。项目结构树如下:
    在这里插入图片描述

8、运行测试

  • 运行项目,首先测试事务回滚的,打开浏览器,在地址栏输入:localhost:8080/rollback?name=林秋延&age=21
  • 服务器将返回有异常发生并显示异常信息:
    在这里插入图片描述
  • 而数据库中也确实没有任何记录增加:
    在这里插入图片描述
  • 测试事务不回滚,访问 URL localhost:8080/norollback?name=pyc&address=陆丰&age=22
  • 服务器仍然返回有异常
    在这里插入图片描述
  • 但是查看数据库,增加了一条数据记录
    在这里插入图片描述

转载地址:http://klqgn.baihongyu.com/

你可能感兴趣的文章
linux sh/bash 编程常用
查看>>
x86寄存器和栈帧
查看>>
计算机科学经典论文(zz)
查看>>
ECC加密算法入门介绍
查看>>
文件系统与NoSQL分布式存储技术对比
查看>>
调试寄存器(debug registers, DRx)理论及实践
查看>>
Linux下逻辑地址-线性地址-物理地址图解
查看>>
vim安装SrcExpl 插件,实现自动显示跳转函数及变量定义功能
查看>>
linux 版本中 i386/i686/x86-64/pcc 等... 的区别
查看>>
机器学习 | 台大林轩田机器学习基石课程笔记11 --- Linear Models for Classification
查看>>
机器学习 | 台大林轩田机器学习基石课程笔记12 --- Nonlinear Transformation
查看>>
线性代数 | (2) 矩阵Part Two
查看>>
机器学习 | 台大林轩田机器学习基石课程笔记13 --- Hazard of Overfitting
查看>>
机器学习 | 台大林轩田机器学习基石课程笔记14 --- Regularization
查看>>
机器学习 | 台大林轩田机器学习基石课程笔记15 --- Validation
查看>>
机器学习 | 台大林轩田机器学习基石课程笔记16 --- Three Learning Principles
查看>>
机器学习 | 台大林轩田机器学习技法课程笔记1 --- Linear Support Vector Machine
查看>>
机器学习 | 台大林轩田机器学习技法课程笔记2 --- Dual Support Vector Machine
查看>>
线性代数 | (3) 行列式
查看>>
学术英语 | (1) wordList1
查看>>