Home Database Mysql Tutorial Dirty read, phantom read, non-repeatable read and lost update instances

Dirty read, phantom read, non-repeatable read and lost update instances

Jun 21, 2017 pm 03:55 PM
lost phantom reading

On June 5, 2017, the weather was rainy.

When I was sorting out my previous study notes two days ago, I found that the concepts of dirty reads, phantom reads, non-repeatable reads and lost updates were a bit vague about the problems caused by transaction concurrency, so I reviewed them again, and now I have summarized some of my understandings as follows for everyone’s convenience.

Locks are a means to prevent other transactions from accessing specified resources. Locks are the main method to achieve concurrency control and are an important guarantee for multiple users to be able to manipulate data in the same database at the same time without data inconsistency. Generally speaking, locks prevent dirty reads, non-repeatable reads, and phantom reads.

1.Dirty Read——A transaction reads data that another transaction has not committed.

Detailed explanation: When a transaction is accessing data and modifying the data, but the modification has not yet been submitted to the database, another transaction also accesses the data and then uses the data. Because this data has not yet been committed, the data read by another transaction is dirty data, and the operations based on the dirty data may be incorrect.

Transaction T1: Update a piece of data
-->Transaction T2: Read the record updated by transaction T1
Transaction T1: Call commit to submit
At this time, transaction T2 reads Data is data stored in database memory, which is called dirty data. This process is called dirty reading.

Dirty reading occurs when one transaction A reads data that has been modified by another transaction B but has not yet been committed. If B rolls back, transaction A reads invalid data. This is similar to a non-repeatable read, but the second transaction does not need to commit.

Solve the dirty read problem: Add an exclusive lock when modifying, and release it after the transaction is committed. Add a shared lock when reading. After reading, release transaction 1 and add a shared lock when reading data (in this way, after the transaction 1 During the process of reading data, other transactions will not modify the data), no transaction is allowed to operate on the data, it can only be read. After 1, if there is an update operation, it will be converted into an exclusive lock, and other transactions will have no rights. Participate in reading and writing, thus preventing dirty read problems. However, when transaction 1 is reading the data, it is possible that other transactions have also read the data. After the reading is completed, the shared lock is released. At this time, transaction 1 modifies the data. After the modification is completed, the transaction is submitted. When other transactions read the data again, the data is found. If they are inconsistent, non-repeatable read problems will occur, so this cannot avoid non-repeatable read problems.

2.Phantom reading (Phantom)——In the same transaction, the same operation is used to read twice, and the number of records obtained is different.

Detailed explanation: Phantom reading refers to a phenomenon that occurs when transactions are not executed independently. For example, the first transaction modifies the data in a table, and this modification involves all the data in the table. OK. At the same time, the second transaction also modifies the data in this table. This modification inserts a row of new data into the table. Then, in the future, the user who operates the first transaction will find that there are still unmodified data rows in the table, as if a hallucination has occurred.

Transaction T1: Query all records in the table
-->Transaction T2: Insert a record
-->Transaction T2: Call commit to submit
Transaction T1: Query the table again All records in
             
At this time, the records queried twice by transaction T1 are different, which is called phantom reading.

Note: Phantom reading focuses on adding or deleting.

Phantom reading occurs when two identical queries are executed, and the result set returned by the second query is different from the first query.

What happens: No scope lock.

How to avoid: Implementing serialization isolation mode may occur in any low-level isolation.

Solving the phantom read problem: The range lock RangeS RangeS_S mode is used to lock the retrieval range as read-only, thus avoiding the phantom read problem.

3.Nonrepeatable Read——In the same transaction, the same data is read twice and the content is different.

Transaction T1: Query a record
-->Transaction T2: Update the record queried by transaction T1
-->Transaction T2: Call commit to submit
Transaction T1: Query again The last record
           
At this time, transaction T1 queried the same data twice, and the obtained content was different, which is called non-repeatable read.

Note: The focus of non-repeatable reading is modification.

In the lock-based parallel control method, if a read lock is not added when executing select, a non-repeatable read problem will occur.

In the multi-version parallel control mechanism, when a transaction that encounters a commit conflict needs to be rolled back but is released, a non-repeatable read problem will occur.

There are two strategies to prevent this problem from happening:

(1) Postpone the execution of transaction 2 until transaction 1 is committed or rolled back. This strategy applies when using locks.

(2) In multi-version parallel control, transaction 2 can be submitted first, while transaction 1 continues to execute on the old version of data. When transaction 1 finally attempts to commit, the database will check whether its result is the same as when transaction 1 and transaction 2 were executed sequentially. If yes, transaction 1 is submitted successfully; if not, transaction 1 will be rolled back.

Solve the non-repeatable read problem: add shared locks when reading data, add exclusive locks when writing data, and release the locks only after transaction submission. No other thing is allowed to modify the data when reading. No matter how many times the data is read during the transaction, the data is consistent, avoiding the problem of non-repeatable reading.
4.Lost Update (Lost Update)

Transaction T1 reads the data, performs some operations, and then updates the data. Transaction T2 also does the same thing, so when T1 and T2 update data, they may overwrite each other's updates, causing errors.

5. To deal with the above isolation level issues, use the following method:

Five levels of transaction isolation:
(1) TRANSACTION_NONE does not use transactions.
(2) TRANSACTION_READ_UNCOMMITTED allows dirty reads.
(3) TRANSACTION_READ_COMMITTED prevents dirty reads, the most commonly used isolation level, and is the default isolation level for most databases.
(4) TRANSACTION_REPEATABLE_READ can prevent dirty reads and non-repeatable reads.
(5) TRANSACTION_SERIALIZABLE can prevent dirty reads, non-repeatable reads and phantom reads, which (transaction serialization) will reduce the efficiency of the database.

The above five transaction isolation levels are static constants defined in the Connection interface. Use the setTransactionIsolation(int level) method to set the transaction isolation level.

For example: con.setTransactionIsolation(Connection.REPEATABLE_READ).

Note: The isolation level of a transaction is limited by the database. The isolation levels supported by different databases are not necessarily the same.

The above is the detailed content of Dirty read, phantom read, non-repeatable read and lost update instances. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve Xlive.dll missing or not found issue in Windows 11/10? How to solve Xlive.dll missing or not found issue in Windows 11/10? May 08, 2023 pm 08:55 PM

<p>Xlive.dll is a dynamic link library (DLL) of Microsoft, which is part of "WindowsLive Games". Errors caused by Xlive.dll can be due to the Xlive.dll file being deleted, misplaced, corrupted by malware, or messed up with registry entries. Not being able to launch a program or game due to this error can be frustrating. Let's see how to solve this problem. This problem can usually be solved by properly reinstalling the Xlive.dll file. </p><p><strong&

Right-click New in Win10 disappears Right-click New in Win10 disappears Jan 07, 2024 pm 03:57 PM

Many friends find that New in the list is missing when they right-click on the desktop. What is going on? It may be that the anti-virus software automatically cleans up some files. In fact, we only need to enter the settings and adjust the values ​​to restore it. , let’s take a look at the specific tutorial with the editor. The first step to restore Win10 by right-clicking and creating a new one, "win+R" to open Run, enter: regedit, click "OK", open the Registry Editor. The second step is to expand HKEY_CLASSES_ROOT in the opened Registry Editor window. In HKEY_CLASSES_ROOT Find: Directory in the expanded items, and then expand: Directory\Background

Solution to d3dx9_43.dll missing in win7 computer Solution to d3dx9_43.dll missing in win7 computer Jul 16, 2023 pm 08:13 PM

How to solve the problem of missing d3dx9_43.dll in win7 computer? 3dx9_43.dll is the dl component of directX9 in computer system components. If you play the game without this component, there will be a message that the game cannot start this program. Users who want to solve the problem should look at the solution for missing d3dx9_43.dl in win7 computers. Solution to d3dx9_43.dll missing in win7 computer. 1. Download or copy the d3dx9_43.dll file_43.dll file from the Internet. Save the d3dx9_43.dll file to the System32 directory in the system; 3. Press Win+R to open the operation and enter regsvr32d3

How to deal with the missing libcurl.dll file on win10 computer How to deal with the missing libcurl.dll file on win10 computer Jan 04, 2024 am 10:17 AM

When we use a computer with the win10 operating system installed, sometimes we may encounter the loss of the libcurl.dll component. For this kind of problem, the editor thinks that we can download such a component from the Internet, then unzip it and install it into our system folder, and then restart the computer. Let the editor tell you how to do the specific steps ~ I hope it can help you. What to do if libcurl.dll is lost in win10 computer 1. Copy libcurl.dll dynamic link library from the C:\Windows\SysWOW64 (64-bit win10) or C:\Windows\System32 (32-bit win10) directory path of other computers with the same system arts

Detailed explanation of the loss of Ubuntu software management functions Detailed explanation of the loss of Ubuntu software management functions Jan 02, 2024 pm 09:18 PM

Most people agree that Ubuntu revolutionized dpkg package management through Ubuntu Software Center. There is no doubt that Software Center is user-friendly by most people's standards. But is this really the case? This article will introduce the current state of Ubuntu software management, how far it has come with the first release of Ubuntu, and where Ubuntu software leads the way. It all starts with apt A pure form of Ubuntu software management uses the apt packaging tool, first built into the great but often underrated Linux distribution Debian. You can use apt to install, remove or update software on Ubuntu. To completely remove the software, you can

How to locate the specific location of a lost Huawei phone? How to locate the specific location of a lost Huawei phone? Mar 24, 2024 am 11:15 AM

In our daily lives, mobile phones have become an indispensable tool for us. We store a large amount of personal information and important data on our mobile phones. Therefore, once the mobile phone is lost or stolen, it will not only cause financial losses, but also may leak private information. For Huawei mobile phone users, how to locate the specific location of their mobile phone after losing it has become an urgent problem that needs to be solved. Huawei mobile phones have a series of intelligent positioning functions, and users can find lost mobile phones in a variety of ways. First, users can use the "Find Mobile Phone" service provided by Huawei to locate

What to do if msvcr110.dll is missing in win10 computer What to do if msvcr110.dll is missing in win10 computer Jul 08, 2023 pm 01:09 PM

When the computer program is opened, the system prompts that the program cannot be started because MSVCR110.dll is missing in the computer. Try to reinstall the program to solve this problem. What should I do if msvcr110.dll is lost in my win10 computer? Since all current 5.5 environments are generated based on vc11 compilation scripts, you must install related components under windows. Let us take a look at the specific methods with the editor. What to do if msvcr110.dll is lost in a win10 computer. First, open the browser and download the relevant program in the browser. After opening, select Simplified Chinese in the language selection, and then click the download button on the right. 2. At this time, a program selection window will pop up asking us to download

What is mysql phantom reading? What is mysql phantom reading? Apr 19, 2023 pm 03:46 PM

In MySQL, phantom reading means that when the user reads a certain range of data rows, another transaction inserts a new row in the range. When the user reads the data rows in the range again, he will find that there is a new " Phantom” line. The so-called phantom read means that the data set queried through SELECT is not a real data set. The user queries through the SELECT statement that a certain record does not exist, but it may exist in the real table.

See all articles