database ACID 和数据库事务如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3740280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do ACID and database transactions work?
提问by never_had_a_name
What is the relationship between ACID and database transaction?
ACID和数据库事务有什么关系?
Does ACID give database transaction or is it the same thing?
ACID 是给数据库事务还是同样的事情?
Could someone enlighten this topic.
有人可以启发这个话题。
回答by Brian Campbell
ACIDis a set of properties that you would like to apply when modifying a database.
ACID是一组您希望在修改数据库时应用的属性。
- Atomicity
- Consistency
- Isolation
- Durability
- 原子性
- 一致性
- 隔离
- 耐用性
A transaction is a set of related changes which is used to achieve some of the ACID properties. Transactions are tools to achieve the ACID properties.
事务是一组相关的更改,用于实现某些 ACID 属性。事务是实现 ACID 属性的工具。
Atomicity means that you can guarantee that all of a transaction happens, or none of it does; you can do complex operations as one single unit, all or nothing, and a crash, power failure, error, or anything else won't allow you to be in a state in which only some of the related changes have happened.
原子性意味着你可以保证所有的事务都发生,或者都不发生;您可以将复杂的操作作为一个单独的单元进行,可以全部执行,也可以不执行,并且崩溃、电源故障、错误或其他任何事情都不会让您处于只发生了一些相关更改的状态。
Consistency means that you guarantee that your data will be consistent; none of the constraints you have on related data will ever be violated.
一致性意味着您保证您的数据将是一致的;您对相关数据的任何限制都不会被违反。
Isolation means that one transaction cannot read data from another transaction that is not yet completed. If two transactions are executing concurrently, each one will see the world as if they were executing sequentially, and if one needs to read data that is written by another, it will have to wait until the other is finished.
隔离意味着一个事务不能从另一个尚未完成的事务中读取数据。如果两个事务并发执行,每个事务都将看到世界,就好像它们在顺序执行一样,如果一个事务需要读取另一个事务写入的数据,则必须等待另一个事务完成。
Durability means that once a transaction is complete, it is guaranteed that all of the changes have been recorded to a durable medium (such as a hard disk), and the fact that the transaction has been completed is likewise recorded.
持久性是指事务一旦完成,就保证所有的变化都被记录在一个持久的介质(如硬盘)上,事务已经完成的事实也同样被记录下来。
So, transactions are a mechanism for guaranteeing these properties; they are a way of grouping related actions together such that as a whole, a group of operations can be atomic, produce consistent results, be isolated from other operations, and be durably recorded.
因此,事务是一种保证这些属性的机制;它们是将相关操作组合在一起的一种方式,作为一个整体,一组操作可以是原子的,产生一致的结果,与其他操作隔离,并被持久地记录。
回答by Erwin Smout
ACID are desirable properties of any transaction processing engine.
ACID 是任何事务处理引擎的理想属性。
A DBMS is (if it is any good) a particular kind of transaction processing engine that exposes, usually to a very large extent but not quite entirely, those properties.
DBMS 是(如果它有任何好处的话)一种特定类型的事务处理引擎,它通常在很大程度上但不是完全公开这些属性。
But other engines exist that can also expose those properties. The kind of software that used to be called "TP monitors" being a case in point (nowadays' equivalent mostly being web servers).
但是存在其他引擎也可以公开这些属性。曾经被称为“TP 监视器”的那种软件就是一个很好的例子(现在主要是 Web 服务器)。
Such TP monitors can access resources other than a DBMS (e.g. a printer), and still guarantee ACID toward their users. As an example of what ACID might mean when a printer is involved in a transaction:
这样的 TP 监视器可以访问 DBMS 以外的资源(例如打印机),并且仍然保证其用户的 ACID。作为当打印机涉及事务时 ACID 可能意味着什么的示例:
- Atomicity: an entire document gets printed or nothing at all
- Consistency: at end-of-transaction, the paper feed is positioned at top-of-page
- Isolation: no two documents get mixed up while printing
- Durability: the printer can guarantee that it was not "printing" with empty cartridges.
- 原子性:打印整个文档或根本不打印
- 一致性:在交易结束时,进纸位于页面顶部
- 隔离:打印时不会混淆两个文档
- 耐用性:打印机可以保证它不是用空墨盒“打印”的。
回答by Manish Singh
I slightly modified the printer example to make it more explainable
我稍微修改了打印机示例以使其更易于解释
1 document which had 2 pages content was sent to printer
1 个包含 2 页内容的文档已发送到打印机
Transaction- document sent to printer
交易- 文件发送到打印机
- atomicity- printer prints 2 pages of a document or none
- consistency- printer prints half page and the page gets stuck. The printer restarts itself and prints 2 pages with all content
- isolation- while there were too many print outs in progress - printer prints the right content of the document
- durability- while printing, there was a power cut- printer again prints documents without any errors
- 原子性- 打印机打印 2 页文档或不打印
- 一致性- 打印机打印半页,页面卡住。打印机重新启动并打印 2 页包含所有内容
- 隔离- 虽然正在进行太多的打印输出 - 打印机打印文档的正确内容
- 耐用性- 打印时,有一个断电 - 打印机再次打印文件,没有任何错误
Hope this helps someone to get the hang of the concept of ACID
希望这有助于某人掌握 ACID 的概念
回答by Vlad Mihalcea
What is the relationship between ACID and database transaction?
ACID和数据库事务有什么关系?
In a relational database, every SQL statement must execute in the scope of a transaction.
在关系数据库中,每条 SQL 语句都必须在事务范围内执行。
Without defining the transaction boundaries explicitly, the database is going to use an implicit transaction which is wraps around every individual statement.
在没有明确定义事务边界的情况下,数据库将使用一个隐式事务,它包含在每个单独的语句周围。
The implicit transaction begins before the statement is executed and end (commit or rollback) after the statement is executed. The implicit transaction mode is commonly known as autocommit.
隐式事务在语句执行之前开始,在语句执行之后结束(提交或回滚)。隐式事务模式通常称为自动提交。
As explained in this article, a transaction is a collection of read/write operations succeeding only if all contained operations succeed.
如本文所述,事务是仅当所有包含的操作都成功时才成功的读/写操作的集合。
Inherently a transaction is characterized by four properties (commonly referred as ACID):
事务本质上具有四个属性(通常称为 ACID):
- Atomicity
- Consistency
- Isolation
- Durability
- 原子性
- 一致性
- 隔离
- 耐用性
Does ACID give database transaction or is it the same thing?
ACID 是给数据库事务还是同样的事情?
For a relational database system, this is true because the SQL Standard specifies that a transaction should provide the ACID guarantees:
对于关系数据库系统,这是正确的,因为 SQL 标准指定事务应提供 ACID 保证:
Atomicity
原子性
Atomicity takes individual operations and turns them into an all-or-nothing unit of work, succeeding if and only if all contained operations succeed.
原子性接受单个操作并将它们变成一个全有或全无的工作单元,当且仅当所有包含的操作成功时才成功。
A transaction might encapsulate a state change (unless it is a read-only one). A transaction must always leave the system in a consistent state, no matter how many concurrent transactions are interleaved at any given time.
事务可能封装状态更改(除非它是只读的)。无论在任何给定时间有多少并发事务交错,事务都必须始终使系统处于一致状态。
Consistency
一致性
Consistency means that constraints are enforced for every committed transaction. That implies that all Keys, Data types, Checks and Trigger are successful and no constraint violation is triggered.
一致性意味着对每个提交的事务强制执行约束。这意味着所有键、数据类型、检查和触发器都是成功的,并且没有触发约束违规。
Isolation
隔离
Transactions require concurrency control mechanisms, and they guarantee correctness even when being interleaved. Isolation brings us the benefit of hiding uncommitted state changes from the outside world, as failing transactions shouldn't ever corrupt the state of the system. Isolation is achieved through concurrency control using pessimistic or optimistic locking mechanisms.
事务需要并发控制机制,即使在交错时也能保证正确性。隔离为我们带来了对外部世界隐藏未提交状态更改的好处,因为失败的事务不应该破坏系统的状态。隔离是通过使用悲观或乐观锁定机制的并发控制来实现的。
Durability
耐用性
A successful transaction must permanently change the state of a system, and before ending it, the state changes are recorded in a persisted transaction log. If our system is suddenly affected by a system crash or a power outage, then all unfinished committed transactions may be replayed.
一个成功的事务必须永久地改变系统的状态,并且在结束它之前,状态改变被记录在一个持久化的事务日志中。如果我们的系统突然受到系统崩溃或断电的影响,那么所有未完成的提交事务可能会被重放。
For more details about Durability and the Redo Log, check out this article.
有关持久性和重做日志的更多详细信息,请查看这篇文章。
回答by Anvesh
ACID properties are very old and important concept of database theory. I know that you can find lots of posts on this topic, but still I would like to start share answer on this because this is very important topic of RDBMS.
ACID 属性是数据库理论中非常古老且重要的概念。我知道你可以找到很多关于这个主题的帖子,但我仍然想开始分享这个问题的答案,因为这是 RDBMS 的一个非常重要的主题。
Database System plays with lots of different types of transactions where all transaction has certain characteristic. This characteristic is known ACID Properties. ACID Properties take grantee for all database transactions to accomplish all tasks.
数据库系统处理许多不同类型的事务,其中所有事务都具有某些特征。这个特性被称为 ACID 特性。ACID 属性接受所有数据库事务的授权以完成所有任务。
Atomicity : Either commit all or nothing.
原子性:要么全部提交,要么不提交。
Consistency : Make consistent record in terms of validate all rule and constraint of transaction.
一致性:在验证交易的所有规则和约束方面做出一致的记录。
Isolation : Make sure that two transaction is unaware to each other.
隔离:确保两个事务彼此不知道。
Durability : committed data stored forever. Reference taken from this article:
持久性:提交的数据永远存储。 引用自这篇文章:
回答by Will A
To quote Wikipedia:
引用维基百科:
ACID (atomicity, consistency, isolation, durability) is a set of properties that guarantee database transactions are processed reliably.
ACID(原子性、一致性、隔离性、持久性)是一组保证数据库事务可靠处理的属性。
A DBMS that supports transactions will strive to support all of these properties - any commercial DBMS (as well as several open-source DBMSs) provide full ACID 'support' - although it's often possible (for example, with varying isolation levels in MSSQL) to lessen the ACIDness - thus losing the guarantee of fully transactional behaviour.
支持事务的 DBMS 将努力支持所有这些属性——任何商业 DBMS(以及几个开源 DBMS)都提供完整的 ACID“支持”——尽管这通常是可能的(例如,在 MSSQL 中具有不同的隔离级别)减少 ACIDness - 从而失去完全交易行为的保证。
回答by Frans Lundberg
[Gray] introduced the ACD properties for a transaction in 1981. In 1983 [Haerder] added the Isolation property. In my opinion, the ACD properties would be have a more useful set of properties to discuss. One interpretation of Atomicity (that the transaction should be atomic as seen from any client any time) would actually imply the isolation property. The "isolation" property is useful when the transaction is notisolated; when the isolation property is relaxed. In ANSI SQL speak: if the isolation level is weaker then SERIALIZABLE. But when the isolation level is SERIALIZABLE, the isolation property is not really of interest.
[Gray] 于 1981 年为交易引入了 ACD 属性。1983 年 [Haerder] 添加了 Isolation 属性。在我看来,ACD 属性将有一组更有用的属性可供讨论。对原子性的一种解释(任何时候从任何客户端看到的事务都应该是原子的)实际上意味着隔离属性。“隔离”属性在事务未隔离时很有用;当隔离属性放松时。在 ANSI SQL 中说:如果隔离级别较弱,则 SERIALIZABLE。但是当隔离级别是 SERIALIZABLE 时,隔离属性就不是真正感兴趣的了。
I have written more about this in a blog post: "ACID Does Not Make Sense".
我在博客文章“ACID 没有意义”中写了更多关于此的内容。
http://blog.franslundberg.com/2013/12/acid-does-not-make-sense.html
http://blog.franslundberg.com/2013/12/acid-does-not-make-sense.html
[Gray] The Transaction Concept, Jim Gray, 1981. http://research.microsoft.com/en-us/um/people/gray/papers/theTransactionConcept.pdf
[灰色] 交易概念,Jim Gray,1981 年。http ://research.microsoft.com/en-us/um/people/gray/papers/theTransactionConcept.pdf
[Haerder] Principles of Transaction-Oriented Database Recovery, Haerder and Reuter, 1983. http://www.stanford.edu/class/cs340v/papers/recovery.pdf
[Haerder] 面向事务的数据库恢复原理,Haerder 和 Reuter,1983 年 。http://www.stanford.edu/class/cs340v/papers/recovery.pdf
回答by rashedcs
Transaction can be defined as a collection of task that are considered as minimum processing unit. Each minimum processing unit can not be divided further.
事务可以定义为被视为最小处理单元的任务集合。每个最小处理单元不能进一步划分。
All transaction must contain four properties that commonly known as ACID properties. i.e ACID are the group of properties of any transaction.
所有事务都必须包含四个通常称为 ACID 属性的属性。即 ACID 是任何事务的一组属性。
- Atomicity :
- Consistency
- Isolation
- Durability
- 原子性:
- 一致性
- 隔离
- 耐用性