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.
- 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。
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.
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.
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.
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 mybatis
JdbcTransactionTo complete, which is equivalent to not relying on any container. The configuration method is also mentioned on the official website
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
The factory mode is used here. For the native JDBC mode, the
You can see that the commit and rollback operations of database connection are directly used here.
You may think that the implementation of hosting is
ManagedTransactionNo, 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
TransactionFactory, it is attached to
EnvironmentIn, mybatis is through
EnvironmentTo get the corresponding
TransactionFactoryAnd then create the corresponding
Transaction。 This is why we can configure different environments to configure different database information to achieve environmental isolation.
Transaction, it is attached to
ExecutorThe 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.
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 projects
mybatis-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 class
You can see that this is a
FactoryBean, is to get through it
SqlSessionFactoryYes, look directly at the core logic
You can see that the transaction management class implemented by spring is created here
SpringManagedTransactionFactory, and then created a
ConfigurationIn this way, all subsequent mybatis gets when obtaining transactions are
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.