Multi data source transaction integration - mybatis plus

At present, most of what Baidu can search are aop and multi data source management implemented separately. It is complex to use, and it is not very perfect. For example, whether to consider nested transactions and the support method of multi data source transactions, many bloggers are not involved. Most of them are simple Interception Based on the spring framework, Their own implementation not only has imperfect shortcomings, but also poor scalability. Maintenance and upgrading of later versions or supporting more complex functions require major changes and comprehensive testing, and the risk coefficient is also quite high. In the second half of 2021, the high version of mybatis plus has realized the unified submission and rollback management of multi-source transactions by implementing the relevant interfaces and interceptors of spring distributed transactions without the help of external coordinator seata. Next, I will describe its usage and implementation details in detail.

1. First, when using mybatis plus, it should be noted that the names of some configuration items are different from mybatis

The yml configuration of mybatis is as follows

spring:  
  datasource:
    master:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://ip:3306/xx?useAffectedRows=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
      username: xx
      password: xx
      type: com.zaxxer.hikari.HikariDataSource
      minimum-idle: 5
      maximum-pool-size: 15
      auto-commit: true
      idle-timeout: 30000
      pool-name: springHikariCP-master
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: SELECT 1   #Verify on connection acquisition

Mybatis plus is configured as follows

spring:  
  datasource:
    dynamic:
      strict: false #Set strict mode, and false will not start by default After startup, an exception will be thrown when the specified data source is not matched. If not, the default data source will be used
      datasource:
        sysmaster:
          url: jdbc:mysql://ip:3306/xx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
          username: xx
          password: xx
          driver-class-name: com.mysql.cj.jdbc.Driver
          type: com.zaxxer.hikari.HikariDataSource
          hikari:
            min-idle: 5
            max-pool-size: 15
            is-auto-commit: false
            idle-timeout: 30000
            pool-name: springHikariCP
            max-lifetime: 1800000
            connection-timeout: 30000
            connection-test-query: SELECT 1

The change of fields involved is mainly due to the change of the configuration class of the properties of the data source used

The following is the configuration class of HikariCpConfig used by mybatis plus

2. Next, let's look at how to use the multi-source transaction of mybatis plus

maven introduction

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.4.1</version>
</dependency>


<!--Database connection-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

Secondly, the service layer needs to add the annotation @ DSTransactional of multi-source transactions

For the dao layer, add the annotation @ DS("master") of the data source

In this way, the transaction support of multiple data sources is realized. Is it very simple? The main reason is that these things have been integrated into the spring framework in the new version. If there is no integration in the old version, you need to implement multi data source switching and transaction rollback and submission. It is quite troublesome, but with the learning attitude of knowing what it is and why it is, Next, we will give a detailed description of how mybatis plus implements multi-source transactions

3. How to implement transaction management of multiple data sources with mybatis plus

There are two layered steps. Firstly, multiple data sources need to be assembled in the way of key and value. Secondly, aop proxy objects are created to uniformly commit and rollback transactions under multiple data sources. The following two flow charts are summarized by reading the source code.

The following is the process of multi data source assembly

The dataSource of the above process assembly is ItemDataSource, and its structure is as follows

The data source property classes are as follows: DynamicDataSourceProperties

The following is the agent operation flow of the two-stage submission mode

Among them, multi data source transaction processing: first create a global transaction, and then uniformly commit the accrual operation in each connection. If the commit fails, roll back all connections in the current global transaction. The core code is as follows:

 

 

The commit or rollback operation will trigger the threadLocal processing of each connectionProxy

 

 

 

 

 

Keywords: Mybatis Spring mybatis-plus

Added by vapour_ on Thu, 16 Dec 2021 10:05:16 +0200