Spring Principle Transaction Analysis - part of a

Spring Principle Transaction Analysis - part of a

What matters

Transaction: a set of logical operations on the composition of the respective units of the set operation, either all succeed or all fail.

The basic characteristics of the transaction

⑴ atomicity (Atomicity)

Atomicity refers to all operations in the transaction included either all succeed, or all fail rolled back, the operator of the transaction if successful, it must be fully applied to the database if the operation fails it can not have any impact on the database.

⑵ consistency (Consistency)

Consistency means that the transaction must transform the database from one consistent state to another consistent state, that is to say before and after a transaction execution and implementation must be in a consistent state.

Take transfers, if both user A and user B money add up to a total of 5,000, no matter how transfers between A and B, turn several accounts, after the end of the transaction the money add up to two users should have 5000, which is transactional consistency.

⑶ isolation (Isolation)

Isolation when a plurality of users concurrent access to the database, such as when operating with a table, a database for each user transaction open, operation not being disturbed by other transactions, for isolation between a plurality of concurrent transactions.

That is, to achieve such an effect: for any two concurrent transactions T1 and T2, T1 appears in the transaction, T2 or T1 has ended before the start, or started after the end of T1, so each transaction does not feel there are other matters to performing concurrently.

About the transaction isolation provides a variety of database isolation levels, you will be introduced to later.

⑷ persistent (Durability)

Persistence means that once a transaction is committed, then changes to the data in the database is permanent, commit the transaction will not be lost even in the case of a database system experienced a failure of the operation.

For example we use JDBC database operations, after the transaction is committed method, the user is prompted transaction is complete, when we complete the program execution until prompted, you can identify and correct the transaction submitted, even if this time the database there is a problem, it must To complete the full implementation of our business, otherwise it will cause us to see the prompt completion of the transaction, but because of the failure of major database without executing a transaction error.

If you do not consider the isolation caused by security problems

Reading problems
  • Dirty read: A transaction reads data from another uncommitted transactions
  • Non-repeatable read: A transaction update data read another transaction has been submitted, resulting in a transaction multiple queries inconsistent results
  • Dummy read, phantom read: read data insert a transaction of another transaction has been submitted, resulting in a transaction multiple queries inconsistent results.
Write questions
  • Lost update
Reading solve the problem
  • Set the transaction isolation level

  • Read uncommitted: read uncommitted, read any problem can not be resolved.

  • Read committed: Read Committed to address the dirty read, but non-repeatable reads and phantom reads can occur.

  • Repeatable read: Repeatable read, solve dirty reads and non-repeatable reads, but phantom reads can occur.

  • Serializable: Reading solve all the problems.

Spring's transaction management API

PlatformTransactionManager: Platform Transaction Manager

  • Platform Transaction Manager: Interface, it is the real target for the Spring-managed transactions.

  • DataSourceTransactionManager: the underlying transaction management using JDBC

  • HibernateTransactionManager: the underlying transaction management using Hibernate

TransactionDefinition: transaction definition information

  • Transaction Definition: related information used to define transaction isolation level, timeout information, communication behavior , whether or not read-only

TransactionStatus: state of affairs

  • The state of affairs: the state of affairs for the object recorded in the transaction management process.

The relationship between the transaction management API:

Spring transaction management objects when the first transaction manager platform definition information according to the transaction management transaction, the transaction manager process, resulting in various states, these states of recording information to the transaction state.

Spring transaction propagation behavior of

Spring propagation behavior

  • Spring provides propagation behavior of seven kinds of transactions:

  • To ensure that multiple operations in the same transaction

  • PROPAGATION_REQUIRED : The default value, if there is a transaction A, the use of A in the transaction, if A does not create a new transaction, the operation will include it

  • PROPAGATION_SUPPORTS : support services, if there is a transaction A, the use of A in the transaction. A If there is no transaction, the transaction is not used.

  • PROPAGATION_MANDATORY : A If there is a transaction, use the A transactions. A If there is no transaction, throw an exception.

Ensure more operations are not the same transaction

  • PROPAGATION_REQUIRES_NEW : If there is a transaction A, the pending transaction A (pause), create a new transaction, include only its own operations. If A is not a transaction, create a new transaction, including its own operations.

  • PROPAGATION_NOT_SUPPORTED : A If there is a transaction, the transaction A pending. Do not use transaction management.

  • PROPAGATION_NEVER : A If there is a transaction, reported abnormal.

  • Nested transactions

  • PROPAGATION_NESTED : nested transaction, if the transaction A, A is performed in accordance with the transaction, after the execution is completed, setting a savepoint, an operation B, if there is no abnormality, is performed by, if abnormal, may choose to roll back to the original starting position, you can also roll back to the save point.

Transaction Control Classification

Programmatic transaction control

Manual control their own affairs, called programmatic transaction control.

Jdbc Code:

Conn.setAutoCommite (false); // set the manual control transaction

Hibernate code:

Session.beginTransaction (); // start a transaction

[Fine-grained transaction control: You can specify the method, add a few lines of the specified transaction control method]

(More flexible, but more cumbersome development: open every time, commit, rollback.)

Declarative transaction control

Spring provides management of affairs, this is called declarative transaction management.

Spring provides an implementation for transaction control. If users want to use Spring's declarative transaction management, should be configured in the configuration file; simply remove the configuration when not want to use. This achieved the greatest degree of control of the affairs of decoupling.

Spring declarative transaction management, core implementation is based Aop.

[Coarse-grained transaction control: only application method to the entire transaction, not a few lines on the method of application transactions. ]

(Because aop intercept method.)

Spring declarative transaction management class:

Jdbc technology: DataSourceTransactionManager

​ Hibernate

Technology: HibernateTransactionManager

Guess you like

Origin blog.csdn.net/weixin_40160543/article/details/92249869