Table of Contents
repeatable read
禁止不可重复读取和脏读取,但是有时可能出现幻影数据
Serializable
提供严格的事务隔离。它要求事务序列化执行,事务只能一个接着一个地执行,但不能并发执行。
Home Database Mysql Tutorial 数据库事务的隔离级别

数据库事务的隔离级别

Jun 07, 2016 pm 03:58 PM
main affairs database level solve software isolation need question

数据库软件需要解决的主要问题为数据一致性和访问并发性之间的矛盾,在理论上人们总结出保证数据一致性需要解决的几种不确定性情况,并以此为基础提出了数据库事务的几种隔离级别。 首先,来看一下,解决数据一致性的过程中,方法会发生的几种不确定情况。

数据库软件需要解决的主要问题为“数据一致性”和“访问并发性”之间的矛盾,在理论上人们总结出保证数据一致性需要解决的几种不确定性情况,并以此为基础提出了数据库事务的几种隔离级别。

首先,来看一下,解决数据一致性的过程中,方法会发生的几种不确定情况。

脏读:

脏读又称无效数据的读出,是指在数据库访问的过程中,事务T1对某一值进行了修改,在没有提交之前,事务T2可以读取到事务T1的修改,由于T1事务还没有提交,因此T2读到的T1事务数据时无效的,即该数据为脏数据。

不可重复读:

在一个事务内,多次读同一个数据。在这个事务还没有结束时,另一个事务也访问该同一数据。那么,在第一个事务的两次读数据之间。由于第二个事务的修改,那么第一个事务读到的数据可能不一样,这样就发生了在一个事务内两次读到的数据是不一样的,因此称为不可重复读,即原始读取不可重复

通过将事务的隔离级别设置为repeatable read可以防止不可重复读,在oracle中,我们可以通过select for update达到同样的效果。

幻读:

情况1:

第一个事务对一个表中的数据进行了修改,比如这种修改涉及到表中的“全部数据行”。同时,第二个事务也修改这个表中的数据,这种修改是向表中插入“一行新数据”。那么,以后就会发生操作第一个事务的用户发现表中还有没有修改的数据行,就好象发生了幻觉一样.

情况2:

事务T1查询表t1(5条)和t2(10条)中的记录总数之和(应为15),在事务T1查询完表t1之后并在查询t2 之前,事务T2向t1和t2中分别插入两条记录,并提交,随后事务T1查询t2中的记录总数,并得出结果t1=5 t2=12,总记录数17,实际的记录总数应为15或者19,不存在17,就像出现了幻觉

解决幻读需要将事务的隔离级别设置为serialization,即串行化

为了避免出现上面的几种情况,在标准SQL规范中,定义了4个事务隔离级别。

Read Uncommited

为了避免允许脏读取,但不允许更新丢失。如果一个事务已经开始写数据,则另外一个事务则不允许同时进行写操作,但允许其他事务读此行数据

Read Commited

允许不可重复读取,但不允许脏读取。

repeatable read

禁止不可重复读取和脏读取,但是有时可能出现幻影数据

Serializable

提供严格的事务隔离。它要求事务序列化执行,事务只能一个接着一个地执行,但不能并发执行。

√: 可能出现 ×: 不会出现

  脏读 不可重复读 幻读
Read uncommitted
Read committed ×
Repeatable read × ×
Serializable × × ×
在oracle数据库中,存在READ COMMITED(默认),Serializable,readonly 三种隔离级别。不同的数据库软件对隔离级别的实现方式是不同的,实现的程度也是不同的,需要具体情况具体分析。
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)

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

All software will be opened with WPS, and the exe default opening method will be restored. All software will be opened with WPS, and the exe default opening method will be restored. Jun 19, 2024 am 01:48 AM

All the software on my friend's computer has been opened using WPS and cannot run normally. All exes cannot be opened, including the task manager, registry, control panel, settings, etc. When opened, all WPS garbled characters appear. This situation cannot be done remotely. The remote software is also an exe, which seems to be unsolvable. Let’s take a look at how 20 operates to restore the computer to normal. This is because the opening method of the exe has been changed to WPS, and you only need to restore the default opening method. Er0 exports the exe registry information on a normal computer and puts it on the website. Because the browser can be opened, please guide your friends to open our website, copy the registry information, create a new text document on the desktop, and save it as [File name: 1.reg; Save type: All files (*.

Samsung Galaxy Z Flip6 review: Simple design and practical experience, is the answer to the discounted version? ! Samsung Galaxy Z Flip6 review: Simple design and practical experience, is the answer to the discounted version? ! Jul 30, 2024 pm 12:54 PM

In the field of folding screens, small folding screens are also loved by many young users due to their lightweight, portable, exquisite and compact fashion attributes. In the previous review of the Samsung Galaxy Z Fold6 large folding screen, I gave it a "more square and more AI" evaluation. The small folding screen released at the same time, Samsung Galaxy Z Flip 6, has also attracted much attention. So what will it be like? Today, let’s unlock this new fashion product together. "Light" design: The fashionable appearance on the fingertips is the same as Galaxy Z Fold 6. The Galaxy Z Flip 6 body adopts a square design. In the unfolded state, the fuselage is slender than the average candy bar machine. The front and rear are connected by a straight-sided middle frame, and the four R corners retain a rounded shape.

Tsinghua Optics AI appears in Nature! Physical neural network, backpropagation is no longer needed Tsinghua Optics AI appears in Nature! Physical neural network, backpropagation is no longer needed Aug 10, 2024 pm 10:15 PM

Using light to train neural networks, Tsinghua University results were recently published in Nature! What should I do if I cannot apply the backpropagation algorithm? They proposed a Fully Forward Mode (FFM) training method that directly performs the training process in the physical optical system, overcoming the limitations of traditional digital computer simulations. To put it simply, it used to be necessary to model the physical system in detail and then simulate these models on a computer to train the network. The FFM method eliminates the modeling process and allows the system to directly use experimental data for learning and optimization. This also means that training no longer needs to check each layer from back to front (backpropagation), but can directly update the parameters of the network from front to back. To use an analogy, like a puzzle, backpropagation

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

See all articles