Transaction Management Mechanism of SpringBook

Keywords: Database Spring Programming SpringBoot

What is a transaction?

Database transaction refers to a series of operations performed as a single logical unit of work, either completely or not. Transaction processing can ensure that data-oriented resources will not be permanently updated unless all operations in the transactional unit are successfully completed. To become a transaction, a logical unit of work must satisfy the so-called ACID (Atomicity, Consistency, Isolation and Persistence) attributes. Transaction is the logical unit of work in the operation of the database, and the transaction management subsystem in the database is responsible for the transaction processing.

SpringBook transaction management mechanism

  • spring transaction management is divided into two ways:

  • Programming transaction refers to the realization of transaction by encoding.
  • Declarative transaction, based on AOP, decouples specific business logic from transaction processing.

What are declarative transactions?

Declarative transaction is based on AOP mechanism. Its essence is to intercept the method before and after, then create or join a transaction before the target method starts, and submit or roll back the transaction according to the implementation after the target method is executed.

The greatest advantage of declarative transaction is that it decouples the specific business logic and transaction processing through AOP mechanism, and does not need to manage transactions by programming, so it does not need to mix transaction management code in business logic code, so declarative transaction is used more in practice.

Declarative transactions have two ways: one is to declare relevant transaction rules in an XML configuration file; the other is to apply transaction rules to business logic based on @Transactional annotations (the @Transactional annotation comes from the org.spring framework.transaction.annotation package).

The configuration without springboot transaction management mechanism is as follows

 <!-- transaction management -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- Configure transaction notification properties -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- Define transaction propagation properties -->
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="import*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="upd*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="set*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!-- Configuring Transaction Aspects -->
    <aop:config>
        <aop:pointcut id="serviceOperation"
                      expression="(execution(* com.ssm.demo.service.*.*(..)))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>

From this code, we can also see the configuration process of declarative transactions:

  1. Configuring Transaction Manager
  2. Configure transaction notification properties
  3. Configuring Transaction Aspects

After this configuration, the related methods are integrated into transaction management when they are executed, and once an exception occurs, the transaction will be rolled back correctly.

In SpringBoot, it is recommended to use annotation @Transactional to control transactions, just add @Transactional annotations to methods or classes that need transaction management. Let's take a look at this simple method.

General Service processing is carried out at the service level. The example code is as follows:

@Service
public class TransactionTestService {
    @Resource
    UserDao userDao;
//This is the code without adding a transaction management mechanism
    public Boolean demo1() {
        User user = new User();
        user.setPassword("password1");
        user.setName("demo1");
        // Add a new record to the database table
        userDao.insertUser(user);
        // exception occurred
        System.out.println(1 / 0);
        return true;
    }

    @Transactional
    public Boolean tdemo2() {
        User user = new User();
        user.setPassword("password2");
        user.setName("demo2");
        // Add a new record to the database table
        userDao.insertUser(user);
        // exception occurred
        System.out.println(1 / 0);
        return true;
    }
}

Sum up

When the target method of @Transactional is declared by the application system call, the Spring Framework defaults to AOP proxy and generates a proxy object at code run time. According to the property configuration information of @Transactional, the proxy object decides whether the target method of @Transactional is determined by the interceptor Transacti. The onInterceptor uses interception. When the Transaction Interceptor intercepts, transactions are created and joined before the target method starts executing, and the logic of the target method is executed. Finally, according to whether the execution is abnormal, the abstract transaction manager AbstractPlatform Transaction Manager is used to operate the data source DataSource. Submit or roll back transactions

Posted by auro on Thu, 19 Sep 2019 01:04:49 -0700