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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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 (*.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

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

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

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.

How does Java database connection handle transactions and concurrency? How does Java database connection handle transactions and concurrency? Apr 16, 2024 am 11:42 AM

Transactions ensure database data integrity, including atomicity, consistency, isolation, and durability. JDBC uses the Connection interface to provide transaction control (setAutoCommit, commit, rollback). Concurrency control mechanisms coordinate concurrent operations, using locks or optimistic/pessimistic concurrency control to achieve transaction isolation to prevent data inconsistencies.

Apple iOS 17.5 latest beta version released! Add web distribution functionality Apple iOS 17.5 latest beta version released! Add web distribution functionality Apr 17, 2024 pm 03:52 PM

Apple has released the second round of developer beta versions of iOS17.5, iPadOS17.5, tvOS17.5, watchOS10.5 and macOS Sonoma14.5, among which iOS17.5 introduces the Apple WebDistribution system. Developers can obtain new versions through the Apple Developer Center, and public users can register to participate in public testing through the Apple Beta Software Program website. The internal version numbers of the new versions are: 21F5058e (replacing 21F5048f) for iOS 17.5 and iPadOS 17.5, 21L5553e (replacing 21L55 for tvOS 17.5 and HomePod Software 17.5).

See all articles