Detailed Explanation of Spring Things (Transaction)

1. Spring transaction overview

Transaction: A program execution unit (unit) that accesses and possibly updates various data items in the database, usually written by a user program written in an advanced database manipulation language or programming language (such as SQL, C++, or Java) caused by execution. When the data is successfully changed in the database, the data changed in the transaction will be submitted and will not be changed. Otherwise, the transaction is canceled or rolled back, and the changes have no effect.

Transaction is an important part of enterprise applications based on relational database (RDBMS). In the field of software development, transaction players play a very important role to ensure the integrity and consistency of application data.

For example, if Zhang San transfers 1,000 yuan to Li Si, then when operating the database, first subtract 1,000 yuan from Zhang San's account, and then add 1,000 yuan to Li Si's account, two-part operation Putting them together is a complete transfer process, which can also be called a transaction.

To put it simply, it is to ensure that a series of database operations succeed and fail at the data layer (commit and rollback).

Second, the characteristics of the transaction

Transactions have four characteristics: atomicity, consistency, isolation, and durability, referred to as ACID characteristics.

  • Atomicity : All operations in a transaction are either all completed or not completed, and will not end at a certain link in the middle. If an error occurs during the execution of the transaction, it will be rolled back (Rollback) to the state before the transaction started, as if the transaction had never been executed.
  • Consistency : The integrity of the database is not compromised before the transaction begins and after the transaction ends.
  • Transaction isolation (Isolation) : The database allows multiple concurrent transactions to read, write and modify its data at the same time. Isolation can prevent data inconsistency caused by cross-execution when multiple transactions are executed concurrently.
  • Durability : After the transaction processing ends, the modification to the data is permanent, even if the system fails, it will not be lost.

3. Transaction isolation level:

1. Dirty reading
Dirty reading refers to reading data in another uncommitted transaction during one transaction processing.

2. Non-repeatable read
Non-repeatable read means that for a certain data in the database, multiple queries within the scope of a transaction return different data values. This is because it was modified and submitted by another transaction during the query interval.

3. Phantom reading: When a transaction queries the same range twice, the latter query sees rows that were not seen in the previous query. If snapshot reads are used in all transactions, phantom reads will not occur, but a mix of snapshot reads and current reads will result in phantom reads.

Transaction isolation is divided into four different levels, including:

  • Read uncommitted (Read uncommitted), the lowest isolation level, allows "dirty reads" (dirty reads), and transactions can see the "not yet committed" modifications of other transactions. If another transaction rolls back, the data read by the current transaction is dirty.
  • Committing to read (read committed), a transaction may encounter the problem of non-repeatable read (Non Repeatable Read). Non-repeatable reading refers to reading the same data multiple times within a transaction. If another transaction happens to modify the data before the transaction ends, then, in the first transaction, the data read twice It may not be consistent.
  • Repeatable read (repeatable read), a transaction may encounter the problem of phantom read (Phantom Read). Phantom reading means that in a transaction, the first time a record is queried, it is found that there is no record, but when trying to update this record that does not exist, it can be successful, and when the same record is read again, it is magical appeared.
  • Serializable, the strictest isolation level, all transactions are executed sequentially, so dirty reads, non-repeatable reads, and phantom reads will not occur. Although the transactions under the Serializable isolation level have the highest security, but because the transactions are executed serially, the efficiency will be greatly reduced, and the performance of the application will be drastically reduced. If there is no particularly important situation, the Serializable isolation level is generally not used.

 

It is important to note that whether a transaction can take effect depends on whether the database engine supports transactions. MySQL's InnoDB engine supports transactions, but MyISAM does not.

Probably the most common transaction-related example in the real world is bank transfers. Suppose we need to transfer 100 yuan from account A to account B. This transfer operation involves the following two operations.
Deduct 100 yuan from account A;
deposit 100 yuan into account B.

If account A successfully deducts 100 yuan, but fails to deposit to account B, then we will lose 100 yuan out of thin air; if account A fails to debit money, but successfully deposits 100 yuan to account B, we will If there is an extra 100 yuan in the account, the bank will suffer losses. Therefore, we must ensure that all operations in the transaction are either all successful or all fail. Understanding this, we have grasped the core of the transaction.

As an excellent open source framework and application platform, Spring also provides good support for transactions. With the help of the powerful configuration capabilities of the IOC container, Spring provides rich functional support for transactions.

Four, Spring transaction support

Spring supports two types of transactions, namely programmatic transactions and declarative transactions.

programmatic transaction

Programmatic transactions refer to embedding transaction management codes into business codes to control the commit and rollback of transactions.

declarative transaction

Declarative transactions separate transaction management code from business methods, and implement transaction management in a declarative manner. For developers, declarative transactions are obviously easier and better to use than programmatic transactions.
In order to achieve the separation of transaction management and business code, you must use AOP in Spring. Its essence is to intercept the method before and after, and then create or join a transaction before the target method starts. After the target method is executed, according to the execution commit or rollback.

Although declarative transactions are better than programmatic transactions, they also have shortcomings. The granularity of declarative transaction management is at the method level, while programmatic transactions can be accurate to the code block level.

Use JDBC to access the database, and mybatis to access the database.

(1) jdbc accesses data and processes transactions. Connection conn; conn.commit();conn.rollback();
(2) mybatis accesses the database and processes transactions. SqlSession.commit() ;SqlSession.rollback();

5. Spring transaction manager

Various transaction managers are provided in the Spring framework for transaction management. Spring's transaction manager is implemented based on AOP. In Spring's transaction manager, it includes configuration transaction propagation behavior, isolation level, read-only and timeout attributes, which provide methods and description strategies for transaction applications.
Layered patterns are often used in Java EE project development. Spring's transaction processing is located in the business logic layer, which provides a solution for transactions.
Spring's transaction management interface

Three core interfaces of transaction management are included in Spring's transaction module (spring-tx-5.2.7.RELEASE.jar).

(1) PlatformTransactionManager interface

The PlatformTransactionManager interface is a transaction manager interface provided by Spring for managing transactions. Spring encapsulates the configuration details of the transaction into the TransactionDefinition object, and then obtains the transaction status (TransactionStatus) through the getTransaction() method of the transaction manager, and performs the next operation on the transaction.

This interface provides three transaction operation methods, as follows:

(2) TransactionDefinition interface

The TransactionDefinition interface is an object of transaction definition (description), which provides methods for obtaining transaction-related information, including five operations, as follows:

(3).TransactionStatus interface

The TransactionStatus interface is the status of the transaction, which describes the status information of the transaction at a certain point in time. It includes six operations, as follows:

Guess you like

Origin blog.csdn.net/weixin_67224308/article/details/130074519