spring (three) - transaction management

1. Spring transaction management

  Transaction management: The essence is to use spring to manage transactions and complete the database's support for transactions.

  Transaction: A set of operations on the database, an operation error, all must be rolled back, which is characterized by acid.

(1 ) There is a problem with transaction concurrency:

  Dirty read: The transaction read data invalid. Transaction T1 modifies a value, then transaction T2 reads the value, after which T1 undoes the modification of the value for some reason.

  Non-repeatable read: Two identical queries within the scope of a transaction return different data. Caused by commits modified by other transactions in the system at query time. Focus on the modification of the same data.

  Phantom read: Multiple transactions operate on a unified dataset. Under the same conditions, due to the modification of transaction T2, the number of records read for the first time and the second time is different. Focus on adding and deleting data sets.

  Lost Update: Transaction T2 operations overwrite transaction T1 operations.

(2 ) Spring transaction management method

  Programmatic transaction management (programmatic transaction management uses TransactionTemplate or directly uses the underlying PlatformTransactionManager), which can achieve fine-grained code block transactions.

  Declarative transaction management: Based on AOP, its essence is to intercept the method before and after, and then create or join a transaction before the target method starts, and commit or roll back the transaction according to the execution status after the target method is executed. There is no need to mix transaction management code with business logic code.

  (including two methods: xml configuration file based on tx and aop namespace/based on @Transactional annotation, annotation on dao layer)

@Transactional (propagation=Propagation.NOT_SUPPORTED) 

public class MyBatisServiceImpl implements MyBatisService {

         @Autowired

         private MyBatisDao dao;

         @Override

         public void insert(Test test) {

                  dao.insert(test); The transaction propagation behavior is PROPAGATION_NOT_SUPPORTED, it runs in a non-transactional mode and will not be stored in the database

                  //Throw unchecked exception, trigger things, rollback  

                  throw new RuntimeException("test");

         }

(3 ) Transaction isolation level

  The degree of isolation between several concurrent transactions.

                  

(4 ) Transaction propagation behavior

  Before starting the current transaction, a transaction context already exists, and there are several options to specify the execution of a transactional method.

                  

 

 

 

 

 

 

 

 

Guess you like

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