Transactional ACID characteristic

Atomicity (Atomicity): A transaction must be regarded as an indivisible minimum unit of work , All operations in the whole transaction are either committed successfully , Or roll back all failed .

uniformity (consistency): Databases always transition from one consistent state to another . Before and after the start of the transaction , The integrity constraint of the database is not broken . For example, it violates uniqueness , The transaction must be revoked , Return to initial state .

Isolation (isolation): The object of each read-write transaction can be separated from the operation object of other transactions , Namely : Transactions are invisible to other transactions before they are committed , It is usually realized by internal locking .

persistence (durability): Once the transaction is committed , Its changes will be permanently saved to the database .

spring Properties of transactions in

Communication behavior :
Support current transaction ; If it doesn't exist , Create a new . This is usually the default setting for transaction definitions , Transaction synchronization scope is usually defined .
Support current transaction ; If no transaction exists , Is executed in a non transactional manner .
Create a new transaction , If current transaction exists , Suspend the current transaction .
Support current transaction ; If the current transaction does not exist , Throw exception .
The current transaction is not supported ; If the current transaction exists , Throw exception .
The current transaction is not supported , There is a transaction pending the current transaction ; Always execute non transactionally .
If the current transaction exists , Is executed in a nested transaction , If there are currently no transactions , similar PROPAGATION_REQUIRED( Create a new ).

Isolation level
1.Read Uncommitted( Read uncommitted content ): Uncommitted data of other transactions may be read .---- Dirty reading problem , Non repeatable reading
2:Read Committed( Read submission ): A transaction can only see the changes made by the committed transaction .---- Non repeatable reading , Unreal reading
3:Repeatable Read( Rereadable ): Multiple consistent unlocked reads in the same transaction , Take the snapshot version established at the first reading (MVCC), It ensures the readability within the same transaction .
4:Serializable( Serialization ): This is the highest level of isolation , It adds a shared lock to each read data row (LOCK IN SHARE
MODE). At this level , It may lead to a large number of timeout phenomena and lock competition .
The difference between unreadable and unreadable :
   Non repeatable reading :( Comparison of read data itself ) After a period of time after a transaction reads some data , Read the data again , It is found that the read data content has changed , It is not repeatable .

   Unreal reading :( Comparison of the number of read result sets ) A transaction queries the previously retrieved data according to the same query criteria , It is found that the number of retrieved result sets increases or decreases ( Inserted by another transaction , Deleted ), Similar to hallucination .

Rollback mechanism (rollbackfor)

Read only (readonly)

Timeout (timeout)

spring Scenario of transaction invalidation in
1: Database level , The database engine itself does not support transactions
2: cover @Transactional The class of the annotated method is not spring Administration
3: cover @Transactional The class of the annotated method is not public of
4: When calling itself , Call the class's own method , Without going through Spring Proxy class for , By default, the transaction will take effect only when it is called externally .
Because by spring When managing spring inside aop The created proxy object will turn off the automatic submission mechanism of the transaction , Then start the transaction .
@Service public class OrderServiceImpl implements OrderService {
@Transactional public void update(Order order) { updateOrder(order); }
@Transactional(propagation = Propagation.REQUIRES_NEW) public void
updateOrder(Order order) { // update order } }
5: The data source does not have a transaction manager configured
6: There is no exception type configured for rollback , Default is RuntimeException, The exception that occurred does not belong to this class .