This is the 11th day of my participation in the August More Text Challenge
MyBatis Transaction Management
< transactionManager type=”JDBC”> This tag represents configuring a transaction manager, and the type attribute indicates the configured transaction type. The JDBC type uses the transaction management mechanism provided by JDBC, while the MANAGED type transfers transaction management to a container, such as Spring. These two types of transaction management are described separately below. For more information on transactions, check out my other article β© # MySQL transactions.
π© Transaction
In MyBatis, transactions are abstracted into a Transation interface, which defines a series of methods for manipulating transactions, including obtaining a connection, committing a transaction, rolling back a transaction, and closing a connection. This interface has three implementation classes, namely JdbcTransaction, ManagedTransaction, and SpringManagedTransaction
- The source code for the Transaction interface is as follows
public interface Transaction {
Connection getConnection() throws SQLException;
void commit() throws SQLException;
void rollback() throws SQLException;
void close() throws SQLException;
Integer getTimeout() throws SQLException;
}
Copy the code
- The class diagram is shown below
Adopt the JDBC transaction management mechanism
Before we look at the JDBC transaction management mechanism, we need to know how MyBatis transaction management mechanism works. MyBatis will generate a TransactionFactory instance based on the type attribute configured in the < transactionManager > tag. MyBatis will generate a TransactionFactory instance based on the type attribute configured in the < transactionManager > tag. TransactionFactory is an interface, its main JdbcTransactionFactory and ManagedTransactionFactory both implementation class, which defines the two ways to create a Transaction, One creates a Transaction based on Connection, and the other creates a Transaction based on Datasource Datasource, Transaction isolation level, and whether or not the Transaction is committed automatically. If type is JDBC, a JdbcTransactionFactory instance is generated, which creates a JdbcTransaction of type JDBC, known as a JdbcTransaction. JdbcTransaction gets the Connection from a DataSource. JdbcTransaction is a wrapper around java.sql.Connection. It is the management of transactions using Connection objects.
- TransactionFactory interface source code
Public interface TransactionFactory {default void setProperties(Properties props) {} // Create a Transaction based on the specified Connection Transaction newTransaction(Connection var1); / / according to the data source, automatic Transaction isolation level, whether to commit the Transaction to create the Transaction Transaction newTransaction (DataSource var1, TransactionIsolationLevel var2, boolean var3); }Copy the code
- TransactionFactory interface implementation class JdbcTransactionFactory source code, used to create JdbcTransaction instance
public class JdbcTransactionFactory implements TransactionFactory { public JdbcTransactionFactory() { } Public Transaction newTransaction(Connection conn) {return new JdbcTransaction(conn); } public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) { return new JdbcTransaction(ds, level, autoCommit); }}Copy the code
- JdbcTransaction implementation class source code
public class JdbcTransaction implements Transaction { protected Connection connection; protected DataSource dataSource; protected TransactionIsolationLevel level; protected boolean autoCommit; Public Connection getConnection() throws SQLException {if (this.connection == null) {this.openConnection(); } return this.connection; Public void commit() throws SQLException {if (this.connection! = null && ! this.connection.getAutoCommit()) { if (log.isDebugEnabled()) { log.debug("Committing JDBC Connection [" + this.connection + "]"); } this.connection.commit(); Public void rollback() throws SQLException {if (this.connection! = null && ! this.connection.getAutoCommit()) { if (log.isDebugEnabled()) { log.debug("Rolling back JDBC Connection [" + this.connection + "]"); } this.connection.rollback(); Public void close() throws SQLException {if (this.connection! = null) { this.resetAutoCommit(); if (log.isDebugEnabled()) { log.debug("Closing JDBC Connection [" + this.connection + "]"); } this.connection.close(); }}... Slightly}Copy the code
MANAGED transaction management mechanism is adopted
MANAGED transaction management mechanism is different from JDBC transaction management mechanism, MANAGED type is the transaction management by the container to deal with, MyBatis itself will not implement transaction management related operations. If our type attribute is set to MANAGED, a ManagedTransaction instance is generated. The commit and ROLLBACK methods in ManagedTransaction are not implemented, so ManagedTransaction allows the container to manage the entire life cycle of a Transaction. Let’s consider how Spring manages transactions if MyBatis is integrated with the Spring framework. For Spring transactions, see my other article β©Spring Transactions
- ManagedTransaction class partial source
public class ManagedTransaction implements Transaction { private DataSource dataSource; private TransactionIsolationLevel level; private Connection connection; private final boolean closeConnection; public Connection getConnection() throws SQLException { if (this.connection == null) { this.openConnection(); } return this.connection; } public void commit() throws SQLException {// No content} public void rollback() throws SQLException {// No content} public void close() throws SQLException { if (this.closeConnection && this.connection ! = null) { if (log.isDebugEnabled()) { log.debug("Closing JDBC Connection [" + this.connection + "]"); } this.connection.close(); }} slightly... }Copy the code
π above is the introduction of MyBatis transaction management, if there is any error, please leave a message to correct, if you think this article is helpful to you then click a like π ππ» π