Java spring-Transaction Management Overview

Source: Internet
Author: User
Tags savepoint

2017-11-11 23:05:39

Transaction (Transaction): A logical set of operations, either all successful or all fail.

I. Characteristics of a transaction

    • atomicity : The business is inseparable
    • consistency : Data integrity remains consistent before and after transaction execution
    • isolation : When a transaction executes, it should not be disturbed by other transactions
    • Persistence : Once finished, the data is permanently saved to the database

if isolation is not considered :
Dirty reads : one transaction reads to another transaction uncommitted data

When a transaction is modifying a data multiple times, and the changes in the transaction are not committed, then a concurrent transaction accesses the data, resulting in inconsistent data from two transactions. For example, user a transfers $100 to User B, and the corresponding SQL command is as follows:

Update account set money=money+100 where name= ' B ';  (At this point A Notice b) Update account set money=money-100 where name= ' A ';

  When only the first SQL is executed, a informs B to view the account, B discovers that the money has been paid (that is, a dirty read occurs), and then regardless of whether the second SQL is executed, as long as the transaction is not committed, all operations will be rolled back, and when B sees the account again later, the money does not actually go.
non-repeatable read : one transaction reads to another transaction has committed data (update) causes a transaction to be inconsistent with multiple query results

  For example, when a transaction T1 reads a certain data, and the transaction T2 modifies the data immediately and commits the transaction to the database, the transaction T1 reads the data again to obtain different results, sending a non-repeatable read.

The difference between non-repeatable reads and dirty reads is that dirty reads are dirty data that a transaction reads from another transaction, and non-repeatable reads the data submitted by the previous transaction.

In some cases, non-repeatable reading is not a problem, for example, we query a data multiple times and of course the results are the result of the final query. But in other cases, problems can occur, such as querying the same data A and B in turn may be different, and A and B may be fighting.
Virtual Read : one transaction read to another transaction has committed data (insert) causes a transaction to be inconsistent with multiple query results

Phantom reading is a phenomenon that occurs when a transaction is not executed independently. For example, a transaction T1 a data item from "1" to "2" for all rows in a table, and the transaction T2 inserts a row of data items into the table, and the value of this data item is "1" and is submitted to the database. The user of the operation transaction T1, if you look at the data you just modified, will find that there is another line that has not been modified, in fact, this line is added from the transaction T2, as if the illusion, this is the occurrence of Phantom read.

Ii. Four isolation levels provided by MySQL database

Serializable (serialization): Can avoid dirty reading, non-repeatable reading, the occurrence of phantom reading.

REPEATABLE READ (repeatable Read): Can avoid dirty read, non-repeatable read occurrence.

③ Read committed (Read Committed): prevents dirty reads from occurring.

④ Read uncommitted (READ UNCOMMITTED): lowest level, no case is guaranteed.

The top four isolation levels are the serializable level, the lowest is the read uncommitted level, and the higher the level, the lower the efficiency of execution. A level like serializable is a lock table (similar to a lock in Java Multi-threading) so that other threads can only wait outside the lock, so what isolation level to choose should be based on the actual situation. The default isolation level in the MySQL database is repeatable READ (repeatable Read).

Third, Spring provides transaction management API

    • Platformtransactionmanager: Platform transaction manager.
Commit (Transactionstatus status) gettransaction (transactiondefinition definition) rollback (transactionstatus status )
    • Transactiondefinition: Transaction definition
ISOLATION_XXX: Transaction ISOLATION level. PROPAGATION_XXX: The propagation behavior of a transaction. (Not in JDBC, in order to solve actual development problems.) Timeout_default: Expiry time
    • Transactionstatus: Transaction status
Whether there is a savepoint whether a new transaction transaction has been committed

Relationship: Platformtransactionmanager sets transaction-related information through transactiondefinition to manage transactions, resulting in some transaction state in the process of managing transactions: The state is logged by the transactionstatus.

Iv. management of transactions three API detailed

    • Platformtransactionmanager: platform transaction manager interface

Spring provides different Platformtransactionmanager interface implementations for different persistence frameworks, the first two of which are most commonly used:

    1. Org.springframework.jdbc.datasource.DataSourceTransactionManager : using spring jdbc or ibatis to persist data
    2. Org.springframework.orm.hibernate3.HibernateTransactionManager : using the Hibernate3.0 version to persist data
    3. Org.springframework.orm.jpa.JpaTransactionManager : Using JPA for persistence
    4. Org.springframework.jdo.JdoTransactionManager : Used when persistence mechanism is JDO
    5. Org.springframework.transaction.jta.JtaTransactionManager : Use a JTA implementation to manage transactions that must be used when a transaction spans multiple resources
    • Transactiondefinition: Transaction Definition

* Isolation LEVEL

    1. isolation_default: The default level. Mysql repeatable_read,oracle read_commited
    2. isolation_read_uncommitted
    3. isolation_read_committed
    4. Isolation_repeatable_read
    5. Isolation_serializable

* Communication behavior of the transaction

Not JDBC transaction management, used to solve real-world development problems.

propagation behavior: Resolves a transactional relationship between business layer calls, there are seven propagation behaviors:

    • propagation_required : Supports the current transaction and creates a new one if it does not exist

* A B If a has a transaction, B uses a transaction, and if a does not have a transaction, it opens a new transaction. (A, B is in a transaction.) )

    • propagation_supports : Supports the current transaction and does not use a transaction if it does not exist

* A B If a has a transaction, B uses a transaction, and if a does not have a transaction, it does not use the transaction.

    • propagation_mandatory : Supports current transaction, throws exception if not present

* A and B if a has a transaction, b. A transaction is used, if a has no transaction, throws an exception.

    • propagation_requires_new : If a transaction exists, suspends the current transaction, creates a new transaction

* A, B if a has a transaction, B. Suspends the transaction of a and re-creates a new transaction. (A, B is not in a transaction. Transactions do not affect each other.)

    • propagation_not_supported : Runs in a non-transactional manner, suspends the current transaction if a transaction exists

* A A, a non-transactional way of running, a has a transaction, will suspend the current transaction.

    • Propagation_never : Runs in a non-transactional manner, throws an exception if a transaction exists
    • propagation_nested : If the current transaction exists, the nested transaction executes

* Based on savepoint technology.
* A, b A has a transaction, after a is executed, the content after the execution of the a transaction is saved to the SAVEPOINT.B transaction has an exception, the user needs their own
Sets the transaction commit or rollback.

Common:
Propagation_required
Propagation_requires_new
propagation_nested

Java spring-Transaction Management Overview

Related Article

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.