Detailed explanation of transaction management examples in spring

This article describes the transaction management in spring as an example. Share with you for your reference. The specific analysis is as follows:

Transaction profile:

Transaction management is an essential technology in enterprise application development to ensure data integrity and consistency

A transaction is a series of actions that are treated as a single unit of work. These actions are either complete or ineffective

Four key attributes of a transaction (acid)

① Atomicity: a transaction room is an atomic operation consisting of a series of actions. The atomicity of the transaction ensures that the actions are either completed or do not work at all. ② consistency: once all the transaction actions are completed, the transaction is committed. Data and resources are in a consistent state that meets business rules ③ isolation: many transactions may process the same data at the same time, so everything should be isolated from other transactions to prevent data corruption ④ durability: once the transaction is completed, no matter what system error occurs, its results should not be affected. Typically, the result of a transaction is written to persistent storage

Transaction management in spring

As an enterprise application framework, spring defines an abstraction layer on top of different transaction management APIs. Application developers can use spring's transaction management mechanism without understanding the underlying transaction management API.

Spring supports both programmatic and declarative transaction management

Programmed transaction management: embed transaction management codes into business methods to control transaction submission and rollback. In programmatic transactions, additional transaction management codes must be included in each business operation

Declarative transaction management: in most cases, it is better than programmatic transaction management. It separates transaction management code from business methods and implements transaction management in a declarative way. As a crosscutting concern, transaction management can be modularized through AOP method. Spring supports declarative transaction management through the spring AOP framework.

Propagation properties of spring transactions:

When a transaction method is called by another transaction method, you must specify how the transaction should propagate. For example, a method may continue to run in an existing transaction, or it may open a new transaction and run in its own transaction.

The propagation behavior of a transaction can be specified by the propagation attribute. Spring defines seven propagation behaviors:

Including promotion_ Required is the default propagation attribute

Problems caused by concurrent transactions

Many unexpected problems may occur when multiple transactions in the same application or different applications execute concurrently on the same dataset.

The problems caused by concurrent transactions can be divided into the following three categories:

① Dirty reads: dirty reads occur when one transaction reads data overwritten by another transaction but not yet committed. If the overwrite is rolled back later, the data obtained by the first transaction is invalid.

② Non repeatable reading: non repeatable reading occurs when a transaction executes the same query twice or more, but gets different data each time. This is usually because another concurrent transaction updates the data during two queries

③ Unreal reading: unreal reading is similar to non repeatable reading. It occurs when a transaction (T1) reads several rows of data, and then another concurrent transaction (T2) inserts some data. In the subsequent query, the first transaction (T1) will find more records that do not exist

Code example

The first is the database table:

Including book (ISBN, book_name, price), account (username, balance), and book_ stock(isbn,stock)

Then there are the classes used:

BookShopDao

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>