Spring transaction management

Posted by el_quijote on Mon, 06 Dec 2021 21:25:21 +0100

Spring transaction management

1. Spring transaction management

Things can be regarded as a unit composed of several operations on the database.

When we develop enterprise applications, an operation for business personnel is actually a combination of multi-step operations for data reading and writing. During the sequential execution of data operations, exceptions may occur in any step of operations, which will lead to the failure of subsequent operations. At this time, due to the incorrect completion of business logic, the previous successful operation of data is not reliable, so it is necessary to fallback in this case.

1.1 role of transaction

In order to ensure that every operation of the user is reliable, every operation in the transaction must be executed successfully. As long as there is an exception, it will fall back to the state where no operation is performed at the beginning of the transaction. These operations are either completed or cancelled, so as to ensure that the data meets the requirements of consistency

1.2 transaction management classification

Transaction management in Spring can be divided into two forms:
Programming transactions are rarely used in projects. In this way, we need to inject a transaction management object TransactionTemplate, and then write our own code to implement when we need to commit or roll back transactions in our code

/*@Autowired
    TransactionTemplate t; When programming, in the code, we need to manually (display) commit rolling transactions like mybatis
     */
 public void save(){
        //t.execute()
        jdbcTemplate.update("insert into admin (account,password,sex)values(?,?,?)","jim1","111","male");
        int a=10/0;
        jdbcTemplate.update("insert into admin (account,password,sex)values(?,?,?)","jim2","111","male");

    } 

Declarative transaction management is based on AOP. Its essence is to intercept before and after methods, so declarative transactions are method level.

1.3 there are two ways of declarative Management:

xml based configuration

Annotation based implementation

Spring Transaction Management API:

PlatformTransactionManager transaction manager interface

Spring provides different implementation classes for different dao frameworks,

The implementation class of JDBC and mybatis transaction management is DataSourceTransactionManager

Configure transaction manager
Xml configuration mode

db.xml:

<!--
       Transaction configuration on declaration spring transaction management
    -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="druidDataSource"></property>
    </bean>

    <!--Implement transaction management configuration in annotation mode-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <!--to configure spring Transaction propagation behavior, which is spring A unique function of the framework-->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="save" expression="execution(* com.ffyc.spring.dao.UserDao.save())"/>
        <aop:advisor advice-ref="txadvice" pointcut-ref="save"></aop:advisor>
    </aop:config>

UserDao:

@Repository(value = "userDao")//<bean id="userDao" class="com.ffyc.spring.dao.UserDao" scope=""></bean>
//@Transactional
public class UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    /*@Autowired
    TransactionTemplate t; When programming, in the code, we need to manually (display) commit rolling transactions like mybatis
     */

    /*
      Database transaction: it is a unit (whole) for several operations on the database

      When we save, it contains two or more operations on the database,
      Then we think that these operations should be a unit (whole),
      This is the atomicity (indivisibility) of transactions. Multiple operations are either successful or not executed
     */

    @Transactional(propagation = Propagation.REQUIRED) //You can also only add a method to explain that this method is carried out in transaction management
    public void save(){
        //t.execute()
        jdbcTemplate.update("insert into admin (account,password,sex)values(?,?,?)","jim1","111","male");
        //int a=10/0;
        jdbcTemplate.update("insert into admin (account,password,sex)values(?,?,?)","jim2","111","male");

    }     //Order payment by transfer is two operations. When there is an exception, there is a problem saving the data

}

There are exceptions. Neither jim1 nor jim2 will come in


When int a=10/0; Comment it out. Both will come in

Annotation method

Control the transaction @ Service(value = "userservice") @ Transactional(propagation=Propagation.REQUIRED) in the service

2. Spring transaction propagation behavior

That is, communication, then there are at least two things that can be transmitted. The behavior of propagation does not exist in monomers.

Transaction propagation behavior refers to how a transaction method should be called by another transaction method.

Transaction propagation behavior is a unique transaction enhancement feature of Spring framework. It does not belong to the actual transaction provider database behavior

For example:
When the methodA transaction method calls the methodB transaction method, does methodB continue to run in the caller's methodA transaction or start a new transaction for itself? This is determined by the transaction propagation behavior of methodB.

Seven communication behaviors

3. Spring integrates Mybatis

To be continued...

Topics: Java Spring Back-end