1. Basic principles of transactions
The essence of Spring transactions is actually the support of transactions by the database. Without the transaction support of the database, spring cannot provide transaction functions. For pure JDBC operation database, if you want to use transactions, you can follow the following steps:
Get the connection Connection con = DriverManager.getConnection()
Open transaction con.setAutoCommit(true/false);
Execute CRUD
Commit transaction/rollback transaction con.commit() / con.rollback();
Close the connection conn.close();
After using Spring’s transaction management function, we can not Then write the code for steps 2 and 4, and let Spirng automatically complete . Then Spring is How to open and close transactions before and after the CRUD we write? To solve this problem, we can understand the implementation principle of Spring's transaction management as a whole. The annotation method is as an example
#.##Configuration fileEnable annotationsDriver, and mark the relevant classes and methods with the @Transactional annotation
##. #spring will parse and generate related beans when it starts. At this time, it will check the classes and methods with relevant annotations, generate proxies for these classes and methods, and inject relevant configurations according to the relevant parameters of @Transaction, so that The agent handles the relevant transactions for us (turn on normal transaction submission and exception rollback). - The real database layer transaction submission and rollback is through binlog or redo. Log implemented.
- 2. Propagation of Spring transactions
The so-called propagation properties of spring transactions are defined when there are multiple transactions at the same time. When they exist, how spring should handle the behavior of these transactions. These attributes are defined in TransactionDefinition. The explanation of the specific
constantsis shown in the following table:
Constant Explanation | ##PROPAGATION_REQUIRED | |||||||||||||||||||||||||||
PROPAGATION_REQUIRES_NEW | ||||||||||||||||||||||||||||
throws an exception | , outer transaction capture, you can also not process the rollback operationPROPAGATION_SUPPORTS | |||||||||||||||||||||||||||
PROPAGATION_MANDATORY | ||||||||||||||||||||||||||||
PROPAGATION_NOT_SUPPORTED | ||||||||||||||||||||||||||||
PROPAGATION_NEVER | ||||||||||||||||||||||||||||
PROPAGATION_NESTED | ||||||||||||||||||||||||||||
If an active transaction exists, run in a nested transaction. If there is no active transaction, the REQUIRED attribute is executed. It uses a single transaction with multiple savepoints that can be rolled back. Rollback of internal transactions will not affect external transactions. It only works on the DataSourceTransactionManager transaction manager. |
Isolation level | Isolation level value | Problems caused |
Read-Uncommitted | 0 | Causes dirty read |
Read-Committed | 1 | Avoid dirty reads, allow non-repeatable reads and phantom reads |
Repeatable-Read | 2 | Avoid dirty reads, non-repeatable reads, Allow phantom reading |
Serializable | 3 | Serialized reading, transactions can only be executed one by one, avoiding dirty reads, non-repeatable reads, Phantom reading. The execution efficiency is slow, so use it with caution |
Dirty read: One transaction added, deleted, or modified data, but it was not committed. Another transaction can read the uncommitted data. If the first transaction is rolled back at this time, the second transaction will read dirty data.
Non-repeatable read: Two read operations occurred in a transaction. Between the first read operation and the second operation, another transaction modified the data. At this time, the data read twice is inconsistent.
Phantom reading: The first transaction batches modifications to a certain range of data, and the second transaction adds a piece of data to this range. At this time, the first transaction will lose the modification of the newly added data.
Summary:
The higher the isolation level, the more complete and consistent the data can be guaranteed, but the greater the impact on concurrency performance.
The default isolation level of most databases is Read Commited, such as SqlServer and Oracle
The default isolation level of a few databases is: Repeatable Read, for example: MySQL InnoDB
4. Isolation levels in Spring
Explanation | |
This is the default isolation level of PlatfromTransactionManager, using the default transaction isolation level of the database. The other four correspond to JDBC isolation levels. | |
This is the lowest isolation level for a transaction, which allows another transaction to see the uncommitted data of this transaction. This isolation level will produce dirty reads, non-repeatable reads and phantom reads. | |
Ensure that the data modified by one transaction can only be read by another transaction after it is submitted. Another transaction cannot read the uncommitted data of this transaction. | |
This transaction isolation level can prevent dirty reads and non-repeatable reads. However, phantom reads may occur. | |
This is the most expensive but most reliable transaction isolation level. Transactions are processed as sequential execution. |
The above is the detailed content of Detailed introduction to Spring transaction principles. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor