[Spring] Spring's transaction management - 1, Spring Transaction Management Overview (database transactions, Spring transaction management core interface)

Spring Transaction Management Overview

Simple recording - a simple record -Java EE enterprise application development tutorial (Spring + Spring MVC + MyBatis) and Spring 3.0 as simple as -Spring transaction management

Spring provides a flexible and convenient transaction management features, but these features are based on the underlying transaction mechanism of the database itself work.

To understand Spring's transaction management and configuration, it is necessary to learn the basics of database transactions.

Database transaction

What is the database transaction it?

To understand the idea of ​​a transaction under the bar.

Affairs of thought:

A lot of complicated things to be step-wise (complex things often split), but they make up a whole, either as a whole to take effect, or overall failure. This idea reflected in the database, that is, multiple SQL statements, or perform all succeed or all fail.

Database transactions:

Database transaction (Database Transaction) refers to a series of database operations as a logical unit of processing operations, database operations ** This unit is either fully executed or not executed at all. ** By a combination of operations related to a set of logic processing unit, error recovery can be simplified, and a more reliable application.

A logic processing unit to be a transaction ACID properties must be satisfied. (Atomic Atomic, consistency Consistency, Isolation Isolation and durability Durabiliy), the so-called ACID following meanings.

  • Atomicity (Atomicity): represents a transaction consisting of multiple database operations is an indivisible atom unit only if all of the operation is successful, the entire transaction was submitted, any transaction in a database operation fails, any operation has been performed must be revoked, so that the database is returned to the initial state. That is, operations within a transaction, either all executed successfully, or all successfully executed.
  • Consistency (Consistency): After transaction execution, in which the state of the database and its business rules are the same, that data will not be destroyed. Such as transfer business, whether the transaction is executed successfully or not, to participate in two transfers and account balances should be constant (the total amount of money the two are the same.).
  • Isolation (Isolation): In concurrent data operations, different transactions have their own data space, their operations do not interfere with each other. Rather, the requirements do not completely interference-free, provides a variety of database transaction isolation level, different isolation levels correspond to the degree of interference, the higher ** isolation level, data consistency is better, but the weaker concurrency. ** each transaction independently. In a concurrent environment, concurrent transactions are isolated from each other, independently of each other.
  • Persistent (Durability): Once the transaction commits successfully, all data operations in the transaction must be persisted to the database, even after the transaction is committed, the database immediately collapse, when the database is restarted, it must be guaranteed by a mechanism to be able to recover data. Once the transaction is committed, the data in the database must be preserved permanently.

In these transactions characteristics, the data "consistency" is the ultimate goal, the other features are the measures to achieve this goal, requirements or means.

A database management system commonly used to ensure the re-execution log atomicity, consistency and durability, re-logging is performed every action database changes, an error occurs after the exit portion of the database to perform operations in a transaction, the database can be re-execution log according i.e. undo operation has been performed. Further, for the transaction it has been presented, even if the database crashes, when restarting the database can be re-perform the corresponding operations on persistent data has not been based on the log.

And Java programs using object-locking mechanism similar thread synchronization, database management system using a database lock isolation mechanisms to ensure the transaction. When multiple transactions are trying to operate on the same data, transaction data only to operate the lock is held until after the previous transaction is completed, the subsequent transactions have a chance to operate on the data . Oracle Database also uses data version of the mechanism, the rollback data for each change will save a version of the changes to the data without affecting the data is read.

What is Spring's transaction management?

What is Spring's transaction management?

In the actual development, database operation will involve the management of affairs, aims to provide a special API for transaction processing Spring. Spring's transaction management simplifies the traditional transaction management processes, and reducing the workload of developers to some extent. Specifically Spring transaction management is provided by the developer when doing database operations, no longer need to manually perform the database commit or rollback operation, and Spring also provides support for transaction propagation, we can achieve more complex transactions inlay logic sets of data consistency provides better support.

Spring transaction management

Spring's support for transaction management

Spring's support for transaction management

Spring provides a consistent programming templates for transaction management, the establishment of a unified high-level transaction abstraction. In other words, regardless of the selected Spring JDBC, Hibernate, JPA, or MyBatis, the Spring so that all users can perform transaction management with a unified programming model .

Like Spring DAO for different persistence implementation provides a template classes, Spring transaction management inherited this style, but also provides a transaction template class the TransactionTemplate . TransactionTemplate and used in conjunction with the transaction callback TransactionCallback specify a specific template persistence operations by transaction type transaction management can be achieved programmatically, without having to focus on access to resources, reuse, release, transaction synchronization and exception handling operations.

Highlights Spring transaction management is declarative transaction management. Spring allows declaratively specified transaction and the transaction boundary IoC configuration attributes, attributes Spring applies transactions in the specified transaction boundary. He EJB declarative transaction is temporary technology, Spring technology make this the civilian population, or even, Spring's declarative transaction is more powerful than EJB.

EJB transaction is based on the JTA, and JTA they must get through JNDI, which means that, regardless of the user's application is applied across data sources, data source or a single application, EJB are required way to use global transactions to be processed, this means that the EJB-based application can not be divorced container provided by the application server environment. Such indiscriminate generalized approach is tantamount to kill a chicken slaughter of cattle are slaughtered with a sledgehammer.

Spring profound understanding: Most applications are based on a single data source, only a few applications need to use JTA transaction multiple data sources. Thus, in the case of a single data source, Spring management services directly underlying data source . In the face of the application of multiple data sources, Spring before seeking support for Java EE application server, application server by reference JNDI resources in a JTA transaction is completed. Spring impressive part is that no matter what the user persistence implementation techniques, and regardless of whether the user uses the JTA transaction, the transaction manager are the same model can be used.

The benefits of this unified approach brings is immeasurable: the user can completely put aside the issue of transaction management programming, and complete the transaction by the configuration management in Spring.

Spring transaction management core interface

Spring all JAR package, the package consists of a JAR called spring-tx-5.2.3.RELEASE, which package is provided for dependencies Spring transaction management. In the JAR package org.springframework.transaction packet, there are three interface file PlatformTransactionManager, TransactionDefinition and TransactionStatus

Here Insert Picture Description

Spring transaction management of three core interface

1.PlatformTransactionManager

2.TransactionDefinition

3.TransactionStatus

TransactionDefinition used to describe the transaction isolation level timeout, whether these transaction attributes may be configured to control the specific transaction attribute read-only transactions and transaction behavior transaction propagation rules provided by XML description or annotations may be provided by manual programming mode . PlatformTransactionManager configured according to the information provided by TransactionDefinition transaction attributes, create a transaction, and the activation state of affairs with TransactionStatus description. The following describes the internal composition of these SPI (Service Provider Interface) interfaces.

https://docs.spring.io/spring/docs/current/javadoc-api/

Platform TransactionManager

PlatformTransactionManager interfaces are provided by Spring platform transaction manager, primarily used to manage the transaction. The transaction interface provides three methods of operation, as follows:

TransactionStatus getTransaction(TransactionDefinition definition);

The definition of the method returns to the transaction from the transaction information in a transaction environment already exists, or create a new transaction, and the transaction is described with TransactionStatus state. (Used to obtain transaction status information)

void commit(TransactionStatus status);

The status of the transaction commits the transaction, if the transaction state has been identified as a rollback-only, the process performs the operation of a transaction rollback. (Used to commit the transaction)

void rollback (TransactionStatus status); The transaction is rolled back. When commit () method throws an exception, rollback () is implicitly called. (Used to roll back the transaction)

In the above three methods, getTransaction (TransactionDefinition definition) method returns an object based TransactionStatus TransactionDefinition parameters, TransactionStatus object is represented by a transaction that is associated with the currently executing thread on.

PlatformTransactionManager interface is just representative of transaction management interface, we do not know how to manage the underlying transactions, specifically how to manage its affairs by the implementation classes to complete. The interface of several common implementation class as follows:

Common Interface implementation class

org.springframework.jdbc.datasource.DataSourceTransactionManager

A data source configured for JDBC transaction manager

org.springframework.orm.hibernate4.HibernateTransactionManager

Hibernate is used to configure the transaction manager

org.springframework.transaction.jta.JtaTransactionManager

It is used to configure global transaction manager

Tip : When the underlying persistence layer using different techniques, systems simply use a different implementation class to PlatformTransactionManager.

TransactionDefinition

Interface TransactionDefinition transaction definition object (described later), the object is defined in the business rules, and provides a method of obtaining information related to the transaction, as follows:

String getName (); get a transaction object name

int getIsolationLevel (); acquisition transaction isolation level

int getPropagationBehavior (); acquisition transaction propagation behavior

int getTimeout (); Gets the transaction timeout

boolean isReadOnly (); Gets whether the read-only transaction

In the above method, transaction propagation behavior refers to the same process, different before and after the transaction operation used. There are many propagation behavior, particularly in the following table:

Here Insert Picture Description

In the transaction management process, the propagation behavior can control whether the transaction needs to be created and how to create a transaction, under normal circumstances, query data will not affect the change in the original data, there is no need for transaction management, and for inserting data, updating and delete operation must be carried out transaction management. If no transaction propagation behavior, Spring default propagation behavior is REQUIRED.

TransactionStatus

TransactionStatus interface is a state of affairs, which describes some point in time state of the transaction information. Organizer of the transaction by acquiring running status information of the interface, it can roll back the transaction indirectly through the interface, compared to the way it rolls back the transaction when an exception is thrown more controllable. The interface extends the interface to SavepointManager, SavepointManager transaction control segment based on the interface savepoint JDBC 3.0 provides a mechanism for nested transactions.

The interface 6 comprises a method, as follows.

void flush (); flush the transaction

boolean hasSavepoint (); Gets whether there is a save point

boolean isCompleted (); Gets whether the transaction is complete

boolean isNewTransaction (); Gets whether the new transaction, if it returns false, indicating that the current transaction is a transaction that already exists, or the current operation does not run in a transaction environment.

boolean isRollbackOnly (); if the acquisition transaction rollback

void setRollbackOnly (); setting transaction is rolled back to the current transaction rollback-only. Identify the notification by the transaction manager can only roll back the transaction, the transaction manager will roll back by explicitly calling the command or thrown way to roll back the transaction.

Transaction management approach

Published 78 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41569732/article/details/104329606