Transaction management of mybatis


This article will talk about the transaction management mechanism of mybatis, which is based on mybatis 3.4.6 and mybatis spring 1.3.2.

Knowledge points

  • What is a transaction
  • Transaction management supported by mybatis
  • Mybatis transaction management implementation mechanism
  • How is spring integrated

What is a transaction

Anyone who has studied SQL must know this concept. Transaction is a very important concept in SQL. Here is a brief introduction.

A transaction consists of one or more basic operations, or a transaction consists of one or more SQL statements. All operations in a transaction are a whole. Either all operations are executed successfully or all operations fail. It has four basic properties, namely atomicity(AConsistency, or indivisibility(CIsolation(IResolution, also known as independence), persistence(DProbabilityACID

1) Atomicity

All SQL statements in a transaction are either executed successfully or failed, and will not end in a middle link. If an error occurs during the execution of a transaction, it will be rolled back to the state before the start of the transaction, as if the transaction had never been executed.

2) Consistency

Before and after the transaction, the integrity of the database is not destroyed. This means that the written data must fully comply with all preset rules, including the accuracy and serialization of the data, and the subsequent database can spontaneously complete the predetermined work.

3) Isolation

The database allows multiple concurrent transactions to read, write and modify their data at the same time. Isolation can prevent data inconsistency caused by cross execution when multiple transactions are executed concurrently. Transaction isolation is divided into different levels, including read uncommitted, read committed, repeatable read and serializable.

4) Persistence

After the transaction is completed, the modification of data is permanent and will not be lost even if the system fails.

Transaction management supported by mybatis

Mybatis currently supports two ways to manage transactions

  • Native JDBC
  • Container hosting

First, let’s introduce the native JDBC. The native JDBC is that we directly configure the corresponding database information in the configuration file of mybatis, and the corresponding transaction related operations are built-in by mybatisJdbcTransactionTo complete, which is equivalent to not relying on any container. The configuration method is also mentioned on the official website
Transaction management of mybatis
Just fill in JDBC for the type here. It should be noted that if you want to use the native JDBC method, it must be implemented through the configuration file, and the spring configuration method is not supported.
Let’s talk about container hosting. As the name suggests, it is to trust the transaction operation to a specific container for implementation. What does container mean? Simply understand, we can think that it is similar to the existence of spring. Combined, mybatis hands over the management and implementation of transactions to spring. One advantage of mybatis is that it can share a database connection with the container without separate management on both sides. Spring’s integration with mybatis is done by implementing a special container hosting class.

Mybatis transaction management implementation mechanism

The implementation of transaction management of mybatis is relatively simple, which is all in this piece
Transaction management of mybatis
The factory mode is used here. For the native JDBC mode, theJdbcTransactionTo manage
Transaction management of mybatis
You can see that the commit and rollback operations of database connection are directly used here.
You may think that the implementation of hosting isManagedTransactionNo, it’s not. Why is there such a class? I guess it only provides sample code here. In fact, it is estimated that it is also compatible with different configurations of mybatis. You can see that it didn’t do anything
Transaction management of mybatis
aboutTransactionFactory, it is attached toEnvironmentIn, mybatis is throughEnvironmentTo get the correspondingTransactionFactoryAnd then create the correspondingTransaction。 This is why we can configure different environments to configure different database information to achieve environmental isolation.
Transaction management of mybatisAnd forTransaction, it is attached toExecutorThe design of the executor is also very easy to understand, because the executor itself is to add, delete, modify and query the database, and it is very reasonable to put transactions into it for management.
Transaction management of mybatis

How is spring integrated

Through the above content, we know that mybatis supports transaction management hosted to the container, and spring is a very classic implementation in this way. Because most of the frameworks we currently use are spring frameworks, we will introduce them when we use mybatis in our projectsmybatis-springThis jar package, which is the key to integrating mybatis with spring, involves a lot of contents, but here we only analyze the transaction management.
For the integration of mybatis, here is a key classSqlSessionFactoryBean
Transaction management of mybatis
You can see that this is aFactoryBean, is to get through itSqlSessionFactoryYes, look directly at the core logicorg.mybatis.spring.SqlSessionFactoryBean#buildSqlSessionFactory
Transaction management of mybatis
You can see that the transaction management class implemented by spring is created hereSpringManagedTransactionFactory, and then created aEnvironmentSet toConfigurationIn this way, all subsequent mybatis gets when obtaining transactions areSpringManagedTransactionFactory
Transaction management of mybatis
All implementations are under this package. Are you familiar with the structure, or the factory mode based on mybatis.


The transaction management implementation of mybatis is relatively simple. In fact, we usually configure specific transaction management classes ourselves, because if we use spring, we basically don’t have to do anything by ourselves, but this design is still good. We directly hand over the relevant modules to the container for processing, and the container access is also simple.