search
HomeDatabaseMysql Tutorial数据库事务四个特性

数据库事务四个特性

Jun 07, 2016 pm 05:39 PM
affairsdatabasecharacteristic

数据库事务四个特性 数据库事务需要满足四个特性,ACID,即原子性(Atomic),一致性(Consistency),隔离性(Isolation),持久性(Durability)。 在关系数据库中,这些特性需要借助借助redo log、锁、snapshot等手段来实现。 隔离性要求各事务之间相互独立,所进

数据库事务四个特性

 

数据库事务需要满足四个特性,ACID,即原子性(Atomic),一致性(Consistency),隔离性(Isolation),,持久性(Durability)。

在关系数据库中,这些特性需要借助借助redo log、锁、snapshot等手段来实现。

隔离性要求各事务之间相互独立,所进行的操作不会相互影响,其用意还是保证各数据读写操作的一致性

标准SQL92规范定义了四种隔离级别,以及相应要禁止的三种现象(Phenomena)。

隔离级别越高,数据一致性的保证越强,但同时数据库的并发能力也越差。  数据库并发操作引起的问题(现象 ) 有:

1. 脏读(Dirty read)。

事务A读取了事务B中尚未提交的数据。如果事务B回滚,则A读取使用了错误的数据。

2. 不可重复度(Unrepeatable read)。

在事务A多次的读取过程中,事务 B对数据进行了修改,导致事务A多次读取的数据不一致。

3. 幻读(Phantom read)。

在事务A多次读取构成中,事务B对数据进行了新增操作,导致事务A多次读取的数据不一致。幻读和不可重复读的区别在于,

不可重复是针对记录的update操作,只要在记录上加写锁,就可避免;幻读是对记录的insert操作,

要禁止幻读必须加上全局的写锁(比如在表上加写锁)。 另外说一下两类丢失更新:

4. 第一类丢失更新(回滚丢失,Lost update)。

在事务A期间,事务 B对数据进行了更新;在事务A撤销之后,覆盖了事务B已经提交的数据。SQL92没有定义这种现象,标准定义的所有隔离界别都不允许第一类丢失更新发生。

5. 第二类丢失更新(覆盖丢失, Second lost update)。

在事务A期间,事务B对数据进行了更新;在事务A提交之后,覆盖了事务B已经提交的数据。第二类丢失更新,实际上和不可重复读是同一种问题。

SQL92定义的四种隔离级别:

1. 未提交读(Read uncommitted)。

写操作加写锁,读操作不加锁。禁止第一类丢失更新,但是会出现所有其他数据并发问题

2.提交读(Read committed)。

写操作加写锁,读操作加读锁。禁止第一类丢失更新和脏读。这是大部分关系数据库的默认 隔离级别。

3.可重复读(Read repeatable)。

对于读操作加读锁到事务结束,其他事务的更新操作只能等到事务结束之后进行。和提交 读的区别在于,

提交读的读操作是加读锁到本次读操作结束,可重复读的锁粒度更大。禁止两类丢失更新,禁止脏读和不可 重复度,但是可能出现幻读.

4.序列化(Serializable)。

读操作加表级读锁至事务结束。可以禁止幻读。

 

                                第一类丢失更新   不可重复读     脏读    第二类丢失更新     幻读

Read uncommitted         禁止               不禁止      不禁止      不禁止         不禁止 

Read committed        禁止                禁止       不禁止      不禁止         不禁止

Read repeatable        禁止                禁止       禁止        禁止           不禁止 

Serializable        禁止                禁止       禁止       禁止             禁止

大多数关系数据库默认使用Read committed的隔离级别,Mysql InnoDB默认使用Read repeatable的隔离级别,

这和Mysql replication 机制使用Statement日志格式有关。各数据库隔离级别的实现也是有差别的,

例如Oracle支持Read committed 和Serializable两种隔离级别,

另外可以通过使用读快照在Read committed级别上禁止不可重复读问题;

Mysql InnoDB在Read repeatable级别上使用next-key locking 策略来避免幻读现象的产生。

 posted on

Statement
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
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools