laitimes

An introduction to the transaction component of the SpringBoot transaction source code

Preface

The source code of the SpringBoot transaction is implemented through the AOP principle, and it is also a related concept with a facet, when the bean to be instantiated has a transaction enhancer, it will generate a proxy object, and then call the notification method to intercept it, that is, the transaction is opened, closed, rolled back, etc. In this article, I will first analyze what components are in the transaction source code.

Transactions are some important component classes

If you're a young man, you're a good man

It stores some information of the @Transaction annotation, such as the isolation level, propagation characteristics, etc., and its implementation is RuleBasedTransactionAttribute, let's take a look at the specific property values of the object, through the following figure, we know that the object is wrapped with the attribute value specified by the user when using the @Transaction annotation

An introduction to the transaction component of the SpringBoot transaction source code

Screenshot of the TransactionAttribute running data

TransactionManager

Transaction Manager, its default implementation is DataSourceTransactionManager, let's see what property values this class has

An introduction to the transaction component of the SpringBoot transaction source code

DataSourceTransactionManager运行数据截图

It can be seen that the transaction manager contains data source information, and it can use the data source to obtain a connection to a database for related data operations.

TransactionStatus

Transaction status object, its default implementation is DefaultTransactionStatus, the object holds some properties during the transaction run, whether it is a new transaction, the transaction save point, whether it needs to be rolled back, and also saves the connection resources of the pending transaction Let's take a look at the property value of the object's running process

An introduction to the transaction component of the SpringBoot transaction source code

DefaultTransactionStatus 行 图

TransactionInfo

One of the most important classes in a transaction is the object, which surrounds the entire transaction execution process and contains the data source object (TransactionManager), the transaction isolation level, the propagation attribute (TransactionAttribute), the current transaction status object (TransactionStatus), and the TransactionInfo of the pending transaction.

An introduction to the transaction component of the SpringBoot transaction source code

Screenshot of the TransactionInfo running data

TransactionInterceptor

At the heart of transaction management, TransactionInterceptor uses Spring AOP to implement method interception. It weaves the transaction management logic into the execution process of the target method, ensuring that the transaction is started before the method is executed, that the transaction is committed or rolled back after the method is executed, and that the TransactionInterceptor rolls back the transaction if the target method throws an unchecked exception.

ConnectionHolder

This object is the Connection object that contains the database connection.

BeanFactoryTransactionAttributeSourceAdvisor

It is a key component of the Spring Framework to support declarative transaction management. It is an Advisor that is primarily used to fetch a TransactionAttribute from Spring's BeanFactory and apply it to a method marked with an @Transactional annotation.

This article mainly briefly explains some of the component classes involved in transaction management, about how they are loaded into the Spring container I will analyze in the next article, if you like my article, please like and follow, we will see you in the next issue.