Learn spring 5 architecture from scratch -- declarative transactions

Keywords: Java Hibernate Spring Transaction acid

affair

  • Transaction is very important in the project development process. It involves the problem of data consistency, which should not be careless!
  • Transaction management is a necessary technology in enterprise application development to ensure data integrity and consistency.

Transaction is to treat a series of actions as an independent unit of work, which are either completed or ineffective.

The four attributes of a transaction are ACID

  • Atomicity: a transaction is an atomic operation consisting of a series of actions. The atomicity of a transaction ensures that the actions are either completely completed or completely ineffective

  • consistency: once all transaction actions are completed, the transaction will be committed. Data and resources are in a consistent state that meets business rules

  • isolation: multiple transactions may process the same data at the same time, so each transaction should be isolated from other transactions to prevent data corruption

  • Persistence: once the transaction is completed, no matter what error occurs in the system, the result will not be affected. Typically, the result of a transaction is written to persistent storage

Transaction management in Spring

Spring defines an abstraction layer on top of different transaction management APIs, so that developers can use spring's transaction management mechanism without understanding the underlying transaction management API. Spring supports programmatic transaction management and declarative transaction management.

Programming transaction management

  • Embed transaction management code into business methods to control transaction commit and rollback
  • Disadvantages:
    Additional transaction management code must be included in each transaction operation business logic

Declarative transaction management

  • Generally, it works better than programmatic transactions.
  • The transaction management code is separated from the business method to realize the transaction management in a declarative way.
  • Transaction management is regarded as a crosscutting concern and modularized through aop method. Spring supports declarative transaction management through the Spring AOP framework.

Managing transactions using Spring

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

Transaction manager

  • No matter which transaction management strategy (programmatic or declarative) Spring uses, the transaction manager is required.

  • Spring's core transaction management abstraction encapsulates a set of technology independent methods.

JDBC transaction

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

After configuring the transaction manager, we need to configure the notification of transactions

<!--Configure transaction notifications-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!--What methods are configured and what transactions are used,Configure propagation properties of transactions-->
        <tx:method name="add" propagation="REQUIRED"/>
        <tx:method name="delete" propagation="REQUIRED"/>
        <tx:method name="update" propagation="REQUIRED"/>
        <tx:method name="search*" propagation="REQUIRED"/>
        <tx:method name="get" read-only="true"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

Features of Spring transaction propagation

Transaction propagation behavior is how transactions propagate among multiple transaction methods when they call each other.
spring supports seven transaction propagation behaviors:

  • propagation_ Requisited: if there is no transaction at present, create a new transaction. If there is already a transaction, join it. This is the most common choice.
  • propagation_supports: supports the current transaction. If there is no current transaction, it will be executed in a non transaction method.
  • propagation_mandatory: use the current transaction. If there is no current transaction, an exception will be thrown.
  • propagation_required_new: create a new transaction. If there is a current transaction, suspend the current transaction.
  • propagation_not_supported: perform operations in a non transactional manner. If there is a transaction, suspend the current transaction.
  • propagation_never: execute the operation in a non transactional manner. If the current transaction exists, an exception will be thrown.
  • propagation_nested: if a transaction currently exists, it is executed within a nested transaction. If there is no transaction at present, the and propagation are executed_ Required similar operations

The default transaction propagation behavior of Spring is PROPAGATION_REQUIRED, which is suitable for most situations.

Configure AOP

Import header file of aop

<!--to configure aop Weaving transaction-->
<aop:config>
    <aop:pointcut id="txPointcut" expression="execution(* com.pag.dao.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

test

@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper mapper = (UserMapper) context.getBean("userDao");
    List<User> user = mapper.selectUser();
    System.out.println(user);
}

Posted by danwguy on Fri, 01 Oct 2021 14:24:27 -0700