[Mybatis source code analysis (four)] the realization principle of mybatis transaction

Mybatis management affairs is divided into three ways:

Transaction interface of mybatis

  1. JdbcTransaction: Use the JDBC transaction management mechanism to complete the transaction commit using the java.sql.Connection object.
  2. ManagedTransaction: Using the Managed transaction management mechanism, mybatis itself will not implement transaction management, but let the container (JBOSS, WebLogic) realize the management of the transaction.
  3. SpringManagedTransaction: Use spring's transaction management mechanism and use @Transaction annotation to achieve

Yu is now a springboot-based project, so I only analyze SpringManagedTransaction

 

First understand some interfaces and classes

1. The TransactionSynchronization interface is Spring's transaction extension interface, a callback interface, used to execute additional logic at important points in the transaction execution process,

2, TransactionSynchronizationManager class: Use ThreadLocal to manage the TransactionSynchronization collection of the current transaction

3. ResourceHolderSynchronization class: it is used to synchronize resources (the concept of resources in Spring transactions), for example: Connection in JDBC, Session in Hibernate, EntiyManager in JPA, and Producer in kafka.

 

In the mybatis, SqlSessionUtil class of internal SqlSessionSynchronization class inherits TransactionSynchronizationAdapter adapter class (equivalent implements TransactionSynchronization Interface )


 

Transaction execution process

1. Mybatis transaction registration process

1. Start with the creation of the JDK dynamic proxy class from the SqlSessionTemplate class, because no matter the way to execute sql, this code will eventually be used.

2. Look directly at the invoke method of SqlSessionInterceptor

It can be seen that the sqlsession is obtained first, and then a certain method of sqlsession is adjusted through reflection, and finally a certain condition is met before the commit is committed.

3. Enter the getSqlSession method

Get resources from the transaction synchronization manager (sqlSessionHolder only wraps the Sqlsession), and get sqlsesison from the sessionHolder.

If there is no sqlsession, create sqlsession through sessionFacory, and then execute registerSessionHolder to register sessionHolder.

4. Enter the registerSessionHoler method

First determine whether the TransactionFactory transaction factory in the Evironment environment class in the Configuration configuration class is SpringManagedTransactionFactory or its subclasses. (In the springboot environment, the default is)

Wrap SqlSessionHolder, and then bind resources to TransactionSynchronizationManager and register TransactionSynchronization

When building SqlSessionFactory in springboot, the transaction factory is set to SpringManagedTransactionFactory.

5. Finally, TransactionSynchronization is registered in the synchronizations static property of the TransactionSynchronizationManager class.

 

Two, spring transaction dynamic proxy process

For the springboot project, why do we use @Transaction to annotate the service method to take effect? How is the bottom layer of spring implemented?

This starts with the @EnableTransactionManagement annotation

1. EnableTransactionManagement has been annotated in the autoconfig package of the springboot project

2. The EnableTransactionManagement annotation Import adds the TransactionManagementConfigurationSelector class, which will be loaded into the container when the spring container is initialized.

3. Since the TransactionManagementConfigurationSelector class implements the ImportSelector interface, the spring container adds these two classes of return values ​​to the container

The ProxyTransactionManagementConfiguration class is mainly used to register and configure the three classes of BeanFactoryTransactionAttributeSourceAdvisor, TransactionAttributeSource, and TransactionInterceptor

4. Continue to look at the AutoProxyRegistrar class, which implements the ImportBeanDefinitionRegistrar interface

So it will execute the registerBeanDefinitions method to register the BeanDefinition.

5. There is a method registerAutoProxyCreatorIfNecessary, which is used to register the InfrastructureAdvisorAutoProxyCreator class

It’s just that its way of registering Beans uses the direct new RootBeanDefinition method, and it also injects the InfrastructureAdvisorAutoProxyCreator class into the spring container.

6. The InfrastructureAdvisorAutoProxyCreator class implements the BeanPostProcessor interface

It is used to process the bean before and after the bean is initialized, so the postProcessBeforeInitialization method will be executed for processing before the bean is instantiated

The aop proxy must be done after the original bean is initialized, and a proxy package is added to the original bean.

7, wrapIfNecessary method, if necessary, create a Proxy proxy

8. The createProxy method is used to create a dynamic proxy and return

 

Three, spring (mybatis) transaction execution process

1. TransactionInterceptor is a transaction method interception executor, which implements the MethodInterceptor interface

2. When the method of annotation @Transaction is called, the invoke method of TransactionInterceptor will be executed, because the class of this method has been proxied by aop.

3. Enter the invokeWithTransaction method

TransactionAttribute: Transaction-related information. Such as annotation type transaction, see if there is an annotation @Transactional on the method acquisition class

PlatformTransactionManager:为Transaction manager

TransactionInfo: Transaction information. It contains transaction manager and transaction related information

proceedWithInvocation: method for invoking the real proxy object service

4. After calling the method of the real proxy object service, commitTransactionAfterReturning is required to commit the transaction

5. Enter the commit method of the transaction manager class

6. Enter the processCommit method

triggerBeforeCommit: used to trigger the beforeCommit method of the TransactionSynchronization class

triggerBeforeCompletion: used to trigger the beforeCompletion method of the TransactionSynchronization class

doCommit: used for the commit of spring transactions

7. Enter the TransactionSynchronizationUtils method to call back the TransactionSynchronization implementation class saved in the synchronizations attribute at the beginning

For example, the SqlSessionSynchronization class defined at the beginning of mybaits

8. Enter the beforeCommit method of the SqlSessionSynchronization class of mybatis

Remove sqlSesion from sqlSessionHolder, execute the commit method to commit the transaction

complete

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/114229703