数据库事务的ACID特性及含义
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库事务的ACID特性及含义相关的知识,希望对你有一定的参考价值。
数据库事务的ACID特性及含义
1、原子性(Atomic)
一个事务被视为一个不可分割的最小工作单元,这个事务里的所有操作要么全部成功执行,要么全都不执行,不能只执行其中的一部分操作。实现事务的原子性,要支持回滚操作,在某个操作失败后,回滚到事务执行之前的状态。
回滚是一个抽象概念,大多数数据库在实现事务时是在事务操作的数据快照上进行,并不修改实际的数据,发生错误时并不提交。
2、 一致性(Consistency)
一致性是指事务使得系统从一个一致性的状态转换到另一个一致性的状态。在实际的工程项目中事务可以有不同程度的一致性:
强一致性:读操作可以立即读到提交的更新数据。
弱一致性:提交的更新操作,不一定立即会被读操作读到,这种情况存在不一致窗口,指的是读操作要延迟一定时间才能读到最新值。
最终一致性:弱一致性的特例。事务更新一份数据,最终一致性保证在没有其他事务更新同样的值的话,最终所有的事务都会读到之前事务更新的最新值。如果没有错误发生,不一致窗口的大小依赖于通信延迟、系统负载等。
其他一致性变体还有:
单调一致性:如果一个进程已经读到一个值,那么后续不会读到更早的值。
会话一致性:保证客户端和服务器交互的会话过程中,读操作可以读到更新操作后的最新值。
3、隔离性(Isolation)
一个事务所做的修改在最终提交以前对其他事务是不可见的。
4、持久性(Durability)
一旦事务提交,则所做的修改会永久保存在数据库中。
数据库事务transanction正确执行的四个基本要素。ACID,原子性(Atomicity)、一致性(Correspondence)、隔离性(Isolation)、持久性(Durability)。
原子性:整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节。事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务从来没有执行过一样。
一致性:在事务开始之前和事务结束以后,数据库的完整性约束没有被破坏。
隔离性:隔离状态执行事务,使它们好像是系统在给定时间内执行的唯一操作。如果有两个事务,运行在相同的时间内,执行 相同的功能,事务的隔离性将确保每一事务在系统中认为只有该事务在使用系统。这种属性有时称为串行化,为了防止事务操作间的混淆,必须串行化或序列化请 求,使得在同一时间仅有一个请求用于同一数据。
持久性:在事务完成以后,该事务所对数据库所作的更改便持久的保存在数据库之中,并不会被回滚。
In computer science, ACID (atomicity, consistency, isolation, durability) is a set of properties of database transactions intended to guarantee data validity despite errors, power failures, and other mishaps. In the context of databases, a sequence of database operations that satisfies the ACID properties (which can be perceived as a single logical operation on the data) is called a transaction. For example, a transfer of funds from one bank account to another, even involving multiple changes such as debiting one account and crediting another, is a single transaction.
ACID (Atomicity, Consistency, Isolation, Durability)
ACID stands for the four English terms Atomicity, Consistency, Isolation and Durability and is a common abbreviation of information technology. In German, the four terms are atomicity, consistency, isolation and permanence. Often in German-speaking countries the acronym AKID is used. The ACID principle establishes rules for handling transactions in database management systems to ensure reliable, consistent data and systems.
A transaction consists of a sequence of different operations that have to comply with these ACID rules. The acronym ACID was already coined in 1983 by the two computer scientists Andreas Reuter and Theo Härder. Standards such as ISO / IEC 10.026-1: 1992 or ISO / IEC 10.026-1: 1998 Section 4 describe the ACID principle. To implement the ACID principle, transaction managers and logging mechanisms are used.
[Related Article: Big Data Phenomenon]
The Four Basic ACID Principles
The ACID concept consists of four individual basic principles. These basic principles are:
- Atomicity or Atomicity: Execution of all or no pieces of information in a transaction
- Consistency or Consistency: Transactions generate a valid state or return to their old state
- Isolation or Demarcation: Transactions of different users or processes remain isolated from each other
- Durability or Durability: After a successful transaction, the data remains permanently stored.
[Related Article: What is Big Data?]
Atomicity :
A transaction consists of a sequence of individual actions. This sequence must proceed in such a way that either all single steps are executed completely or not at all. If errors occur during a sequence, the system must ensure that all changes already made by the transaction are withdrawn.
All information of an interrupted transaction can be removed without leaving the database. The individual database operations of a transaction are only valid once they have been completed. Database systems realize the atomicity by detailed logging of all actions performed.
Consistency :
When a transaction completes successfully, it must leave a consistent state in the previously consistent database. Before completing the transaction, you must check the data integrity and consistency requirements defined in a database. Such conditions may include compliance with certain ranges of values, the presence of key characteristics, or the uniqueness of relationships.
If a transaction violates the consistency conditions, it is rejected and all data is reset to the state before the transaction. Consistency must be ensured before and after a transaction. There may be inconsistent states during the transaction.
Isolation or Demarcation :
Multiple users or processes work simultaneously with one database. You read or write parallel data. Isolation (demarcation) ensures that the use of the database by multiple users has no negative impact.
Events such as the mutual overwriting or deleting of individual data sets can be prevented under all circumstances. For each user, the database management system appears like an exclusively used system in which the transactions do not influence each other and concurrent accesses remain invisible. One also speaks of the integrity of the process. Database systems implement isolation using locking methods.
Durability :
When a transaction is executed and consistent, its information is permanently stored in the database. Future errors, memory failures, or system crashes must not result in data being deleted or made obsolete.
The durability can be realized in a database management system similar to the atomicity by logging measures. With a transaction log, the information after a system failure by reproducing the recorded write operations are reproducible.
Benefits of complying with ACID principles
Adhering to ACID principles creates many benefits when working with databases. Both users and developers can expect a clean environment and consistent data. When a transaction is completed, the availability and permanence of the data is ensured.
Errors during transactions do not result in erroneous or inconsistent information in the database. Elaborate manual research and modification work to clean up errors is not necessary. In multi-user systems, the ACID principle prevents users from interacting with each other. Downstream monitoring of the integrity of the data in the processing applications is usually superfluous.
ACID in distributed systems
In distributed database management systems, compliance with the ACID principle can lead to increased costs. In particular, the parallel operation and data storage in different locations of the distributed systems may be critical to ACID. There are methods such as two-phase commit (2PC) used.
参考:What is ACID Database?
参考:ACID
参考:数据库事务的ACID特性及含义
参考:数据库事务ACID特性及隔离级别
以上是关于数据库事务的ACID特性及含义的主要内容,如果未能解决你的问题,请参考以下文章