Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of java transaction, isolation level of transaction

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the concept of java transaction and the isolation level of transaction". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is a transaction?

Transaction, or database transaction. It is a logical unit in the execution process of database management system, which is composed of a limited sequence of database operations.

Typically, the correct execution of a transaction causes the database to transition from one state to another.

Characteristics of transactions (ACID principle)

Atomicity is indivisibility, and transactions are either executed or not executed at all.

The execution of consistency transactions transforms the database from one correct state to another.

Isolation (isolation) does not allow a transaction to provide changes to that data to any other transaction until the transaction is committed correctly.

When a persistent (durability) transaction commits correctly, the results are saved in the database forever.

Problems caused by transactions in a concurrent state

The concurrency state is interpreted as a lot of problems that may be encountered when transaction An and transaction B operate on the same resource.

Dirty read (for unsubmitted data)

That is, transaction A reads the data that transaction B has not yet committed. If transaction A updates the data, but transaction A does not commit, but transaction B sees the update that transaction A did not commit at this time. When transaction A rolls back, the data just seen by transaction B is dirty data. That is, dirty reading.

Example:

A transferred 1 million to B, but A has not yet submitted, at this time B inquired about his own account, more than 1 million. Then A found that he had turned to the wrong person and rolled back things. And then B 1 million is gone. In the process, B found unsubmitted data (an extra 1 million), which is dirty reading.

Non-repeatable read (read some data twice in a transaction, the data read is inconsistent, for the modification operation)

That is, the same transaction reads the same data many times during the execution of the transaction, but the result of each read is different. The reason is that during the interval between the two reads, the data was modified by others, resulting in inconsistent results of the two reads of the unified transaction.

Example:

An inquires the bank balance is 1 million, B withdraws 500000 at this time, the balance becomes 500000, An inquires the balance again, becomes 500000. For A, the result of two inconsistencies is non-repeatable.

Illusory reading (unmanipulated data is found in an operation in a transaction, aiming at adding and deleting operations)

That is, in the process of reading the dataset many times by transaction A, transaction B adds or deletes the data, which leads to the inconsistency of the dataset read by transaction A.

Example:

When A modifies the information of all the employees in the current company, B inserts a new employee into it. at this time, A finds the information of an employee who has not been modified, which is like an illusion to A.

Isolation level of the transaction

In order to deal with the problems in the concurrency situation above, the isolation level of the transaction is generated. The higher the isolation level of the transaction, the less the above problems, but the greater the performance consumption. Therefore, in the actual production process, the isolation level should be determined according to the demand.

Four isolation level READ_UNCOMMITTED

Read uncommitted, that is, data that is not committed can be read, so it is obvious that this level of isolation mechanism can not solve any kind of dirty reading, unrepeatable reading, or phantom reading.

READ_COMMITED

Submitted, that is, being able to read the data that has been submitted, can prevent dirty reading, but can not solve the problem of unrepeatable reading and phantom reading.

REPEATABLE_READ

Repeat reading, that is, add a lock after the data is read, similar to "select * from XXX for update". It is clear that the data is read for update, so add a lock to prevent others from modifying it. REPEATABLE_READ also means similarly, read a piece of data, other transactions can not change the record until the end of the transaction, so as to solve the problem of dirty reading and unrepeatable reading, but the problem of phantom reading can not be solved.

SERLALIZABLE

Serialization, the highest transaction isolation level, no matter how many transactions, after running all the sub-transactions of one transaction one by one, all the sub-transactions in another transaction can be executed, thus solving the problems of dirty reading, non-repeatable reading and phantom reading.

This is the end of the introduction of "what is the concept of java transaction and the isolation level of transaction". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report