1. Transaction review

(1) What is a transaction?

A transaction is a logical set of operations in which the units of logic that make up the operation either succeed or fail together.

(2) Transaction characteristics (ACID)
  • Atomicity: Emphasizes the indivisibility of transactions
  • Consistency: Data integrity is consistent before and after a transaction is executed
  • Isolation: The execution of a transaction should not be disturbed by other transactions
  • Persistence: Data is persisted to the database once the transaction ends
(3) Security of transaction concurrency
  • Dirty read: a transaction has read uncommitted data from another transaction
  • Non-repeatable read: a transaction reads update data that has been committed by another transaction, causing multiple query results to be inconsistent
  • Phantom read: one transaction reads insert data that another transaction has committed, resulting in inconsistent results for multiple queries
(4) Transaction isolation level
  • Uncommitted reads: Dirty reads, unrepeatable reads, and phantom reads may occur
  • Committed reads: Avoid dirty reads, but non-repeatable reads and phantom reads may occur
  • Repeatable reads: Avoid dirty and unrepeatable reads, but phantom reads may occur
  • Serialization: Avoid all of the above read problems
(5) Default transaction isolation level for common databases
  • MySQL: repeatable read
  • Oracle: Read committed

2. Spring Transaction Management

(1) Basic transaction operation
  • Open the transaction
  • Commit the transaction
  • Roll back the transaction
(2) Transaction operation object

Spring provides the PlatformTransactionManager interface operation transaction in transaction management, one of the most core object is an object TransactionManager.

(3) Spring transaction isolation level
  • Read uncommitted
  • Reading has been submitted
  • Repeatable read
  • serialization
(4) Spring transaction propagation behavior

Ensure that the same transaction

  • PROPAGATION_REQUIRED: Support the current transaction, create a new one if it doesn’t exist (default)
  • PROPAGATION_SUPPORTS: Supports the current transaction, if it does not exist, it is not used
  • PROPAGATION_MANDATORY: The current transaction is supported, and an exception is thrown if it does not exist

Ensure that they are not in the same transaction

  • PROPAGATION_REQUIRES_NEW: If a transaction exists, suspend the current one and create a new one
  • PROPAGATION_NOT_SUPPORTED: Runs non-transactionally, suspending the current transaction if one exists
  • PROPAGATION_NEVER: Runs non-transactionally, throws an exception if a transaction exists
  • PROPAGATION_NESTED: Nested transaction execution if the current transaction exists

3. Spring transaction management

(1) Encoding formula
  • Configure the core transaction manager into the Spring container
<! Transaction core manager, which encapsulates all transaction operations. Depends on the connection pool --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <property name="dataSource" ref="dataSource" ></property>
</bean>
Copy the code
  • Configure the TransactionTemplate template
<! Transaction template object --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
    <property name="transactionManager" ref="transactionManager" ></property>
</bean>
Copy the code
  • Inject the transaction template into the Service
<! Inject the demoDao and transaction template into Service--> <bean name="demoService" class="com.spring.service.demoServiceImpl" >
    <property name="dm" ref="demoDao" ></property>
    <property name="tt" ref="transactionTemplate" ></property>
</bean>  
Copy the code
  • Invoke the template in the Service
(2) XML configuration
  • Import the required JAR packages and constraints

  • Configuring transaction Notification

<! <tx:advice ID ="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <! To separate the methods and to share their propagation behaviorsread-only: indicates whether it is read-only --> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
        <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
    </tx:attributes>
</tx:advice>
Copy the code
  • Weave notifications into the target
<! --> <aop:config > <! Aop :pointcut expression="execution(*com.spring.service.*ServiceImpl.*(..) )" id="txPc"/ > <! Pointcut-ref: advisor-ref = < AOP: Advisor advisor-ref ="txAdvice" pointcut-ref="txPc" />
</aop:config>
Copy the code
(3) Annotation configuration method
  • Import the required JAR packages and constraints

  • Open annotation management transactions

<! <tx:annotation-driven/>Copy the code
  • Use annotations in specific business situations

Scan the official wechat account to learn more