Spring Combat (13) Spring transaction

Source: Internet
Author: User

1. What is a transaction?

A transaction is a logical set of operations, either all succeed or all fail.

  

2. Characteristics of the transaction

Atomicity: A transaction is an inseparable unit of work that either occurs in a transaction or does not occur.

Consistency: The integrity of the data before and after the transaction must be consistent.

Isolation: Multiple users access the database concurrently, the execution of one user's transaction cannot be disturbed by other transactions, and the data between multiple concurrent transactions is isolated from each other.

Persistence: Once a transaction is committed, its changes to the data in the database should be permanent, and should not have any effect even if the database fails.

  

3. What are the issues associated with transactional concurrency operations?

Loss of modification: Two transactions T1 and T2 read the same data and modify, T2 committed results destroy the results of T1 commits, resulting in T1 modification is lost.

Dirty reads: One transaction reads data that another transaction overwrites but has not yet committed, and if those transactions are rolled back, the read is invalid.

Non-repeatable READ: In the same transaction, multiple reads of the same data return a different result (the data changed during reading).

Phantom read: After a transaction has read a few rows of records, another transaction inserts some records, and a phantom read occurs. Later in the query, the first transaction finds some records that were not originally found.

4. Spring Transaction Management API (3 main)

Platformtransactionmanager: Transaction manager;

Transactiondefinition: Definition information of the transaction (isolation, propagation, timeout, read-only);

Transactionstatus: Save transaction specific operational status

5. Platformtransactionmanager transaction Manager

Spring provides different platformtransactionmanager for different persistence frameworks.

You must first create an object of the transaction manager, regardless of the way you use it.

6. Transactiondefinition defines the transaction (isolation, propagation, timeout, read-only)

  Set Isolation Level: (a set of constant definitions)

   

(MySQL default repeatable_read, oracle default read_committed)

  Defining the propagation behavior of a transaction

  Scenario: When we invoke a spring-based service interface method, such as Userservice#adduser (), it will run in a spring-managed transactional environment. The service interface method may internally invoke other service interface methods to work together to complete a complete business operation, resulting in the case of nested calls to the Services interface method, which controls how the current transaction propagates to the target service interface method of the nested invocation through the transaction propagation behavior.

SPRING Specifies 7 types of transactional propagation behavior in the Transactiondefinition interface, which specifies how transactions are propagated when a nested invocation of a transactional method and a transaction method occurs:

(When using propagation_nested, the underlying data source must be based on JDBC 3.0, and the implementation needs to support the savepoint transaction mechanism.) )

transaction propagation behavior type

                         
                         

                         
                          

propagation_required

                         
< Span style= "font-size:15px" >                         

If there is no current transaction, create a new transaction, if one is already present, join the transaction. This is the most common choice.

                         
                          

propagation_supports

                         
< Span style= "font-size:15px" >                         

Supports the current transaction and is executed in a non-transactional manner if no transaction is currently in use.

                         
                          

propagation_mandatory

                         
< Span style= "font-size:15px" >                         

Throws an exception if no transaction is currently in use for the current transaction.

                         
                          

propagation_requires_new

                         
< Span style= "font-size:15px" >                         

new transaction Suspends the current transaction if a transaction is currently present.

                         
                          

propagation_not_supported

                         
< Span style= "font-size:15px" >                         

Executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.

                         
                          

propagation_never

                         
                         

                         
                          

propagation_nested

                         
< Span style= "font-size:15px" >                         

Executes within a nested transaction if the transaction is currently present. If there is currently no transaction, perform a similar operation as propagation_required.

                      &NB sp; 

In addition, there are time-outs, read-only, and other related functions.

7. Transactionstatus: Save Transaction state

The interface provides methods to determine (obtain) the current state of a transaction:

void Flush (); Flush database underlying session

Boolean hassavepoint (); Returns whether the transaction has a save point

Boolean iscompleted (); Returns whether the transaction is committed or rolled back

Boolean isnewtransaction (); Returns whether it is a new transaction

Boolean isrollbackonly (); Determines whether the transaction has rollback-only set.

void Setrollbackonly (); Set this transaction rollback-only.

8. Nested transactions

Nesting is the child transaction set that executes in the parent transaction, which is part of the parent transaction.

Before the child transaction is entered, the parent transaction establishes a rollback point, called Save, and then executes the child transaction. The execution of this child transaction is also considered part of the parent transaction, and the child transaction execution ends and the parent transaction continues execution. The point is that save points, see a few questions to understand.

What happens if a child transaction is rolled back?
The parent transaction rolls back to the save point that was established before entering the child transaction, and then tries other transactions or other business logic, and the actions before the parent transaction are not affected and are not automatically rolled back.

What happens if the parent transaction is rolled back?
The parent transaction is rolled back, and the child transaction is rolled back! Why, because the child transaction is not committed until the end of the parent transaction, we say that the child transaction is part of the parent transaction, and that is the truth.

So: what happens to the commit of the transaction? is the parent transaction committed first, then the child transaction commits, or the child transaction is committed first, and the parent transaction commits again?
The answer is the second case, or the sentence, the child transaction is part of the parent transaction and is submitted uniformly by the parent transaction.

9. Spring supports two ways of transaction management

--Programmatic transaction management

      • Seldom applied in actual development
      • Manually manage things by transactiontemplate

--Declaring transactions using XML configuration

      • Recommended in development (minimal code intrusion)
      • Spring's declarative transactions are implemented through AOP

10. Declarative transaction Management--xml configuration (AOP idea)

STEP-01 configuration transaction Manager-inject datasource to determine which database operation;

step-02 the enhancement of the configuration transaction-the transaction operation to be done to determine the method matching rules for the transaction operation. (Isolation level, which method transactions)

step-03 Configure facets--apply notifications to tangency points.

11. Declarative transaction Management--annotation configuration

STEP-01 configuration transaction Manager-inject datasource to determine which database operation;

step-02 opening transaction annotations;

step-03 Add the annotation @transactional on the class where you want to use the transaction.

Reference: http://opiece.me/2016/03/18/spring-transactional-introduce/

  

Spring Combat (13) Spring transaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.