Spring transaction process analysis

Configure entry from Spring

 <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

<!--配置基于注解的声明式事务,默认使用注解来管理事务行为-->
<tx:annotation-driven transaction-manager="transactionManager"/>

As you can see, we defined a DataSourceTransactionManager when configuring Spring transactions.
What is the function of this class?
From the name, it is "data source transaction manager".
Let's first review how to operate transactions in JDBC:
1. Set AutoCommit to manual commit
2. After all Statements are executed, manually commit
3. An exception occurs When, capture, rollback (you can also rollback to a specified place)

Now, let's look at DataSourceTransactionManager.
We first write a Service, which operates several DAOs, and then add @Transactional
when executing this method, it will wrap a layer of (Spring AOP aspect)
annotations, and then debug into DataSourceTransactionManager. The main process is as follows :
1. doGetTransaction
2. doBegin: Get Connection, set auto-commit to false, and set some basic parameters

3. Execute the method annotated with @Transactional

4.doCommit: transaction commit
5.doRollback: if there is an exception, the transaction will be rolled back

Spring tx aspect process:
cut-in class:
org.springframework.transaction.config.TxNamespaceHandler->init()->AnnotationDrivenBeanDefinitionParser()->configureAutoProxyCreator()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324678046&siteId=291194637