database 什么是数据库事务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/974596/
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
What is a database transaction?
提问by Vlad Gudim
Can someone provide a straightforward (but not simpler than possible) explanation of a transaction as applied to computing (even if copied from Wikipedia)?
有人可以对应用于计算的交易(即使是从维基百科复制的)提供一个简单的(但不比可能的简单)解释吗?
回答by Vilx-
A transaction is a unit of work that you want to treat as "a whole." It has to either happen in full or not at all.
事务是您希望将其视为“整体”的一个工作单元。它要么完全发生,要么根本不发生。
A classical example is transferring money from one bank account to another. To do that you have first to withdraw the amount from the source account, and then deposit it to the destination account. The operation has to succeed in full. If you stop halfway, the money will be lost, and that is Very Bad.
一个经典的例子是从一个银行账户向另一个银行账户转账。为此,您必须首先从源账户提取金额,然后将其存入目标账户。手术必须完全成功。如果你中途停下来,钱就会丢失,那是非常糟糕的。
In modern databases transactions also do some other things - like ensure that you can't access data that another person has written halfway. But the basic idea is the same - transactions are there to ensure, that no matter what happens, the data you work with will be in a sensible state. They guarantee that there will NOT be a situation where money is withdrawn from one account, but not deposited to another.
在现代数据库中,事务还可以做一些其他的事情——比如确保你不能访问其他人中途写入的数据。但基本思想是相同的 - 交易是为了确保无论发生什么,您处理的数据都将处于合理状态。他们保证不会出现从一个帐户中提取资金但未存入另一个帐户的情况。
回答by Jon Skeet
A transaction is a way of representing a state change. Transactions ideally have four properties, commonly known as ACID:
事务是表示状态变化的一种方式。理想情况下,事务具有四个属性,通常称为 ACID:
- Atomic (if the change is committed, it happens in one fell swoop; you can never see "half a change")
- Consistent (the change can only happen if the new state of the system will be valid; any attempt to commit an invalid change will fail, leaving the system in its previous valid state)
- Isolated (no-one else sees any part of the transaction until it's committed)
- Durable (once the change has happened - if the system says the transaction has been committed, the client doesn't need to worry about "flushing" the system to make the change "stick")
- 原子的(如果更改被提交,它会一举发生;你永远看不到“半个更改”)
- 一致(只有当系统的新状态有效时才会发生更改;任何提交无效更改的尝试都将失败,使系统处于先前的有效状态)
- 隔离(在提交之前没有其他人看到事务的任何部分)
- 持久(一旦发生更改 - 如果系统说事务已提交,则客户端无需担心“刷新”系统以使更改“坚持”)
See the Wikipedia ACIDentry for more details.
有关更多详细信息,请参阅维基百科ACID条目。
Although this is typically applied to databases, it doesn't have to be. (In particular, see Software Transactional Memory.)
尽管这通常应用于数据库,但并非必须如此。(特别是,请参阅软件事务内存。)
回答by sharptooth
Here's a simple explanation. You need to transfer 100 bucks from account A to account B. You can either do:
这是一个简单的解释。您需要从 A 账户向 B 账户转账 100 美元。您可以这样做:
accountA -= 100;
accountB += 100;
or
或者
accountB += 100;
accountA -= 100;
If something goes wrong between the first and the second operation in the pair you have a problem - either 100 bucks have disappeared, or they have appeared out of nowhere.
如果这对中的第一次和第二次操作之间出现问题,您就会遇到问题 - 100 美元消失了,或者它们突然出现了。
A transaction is a mechanism that allows you to mark a group of operations and execute them in such a way that either they all execute (commit), or the system state will be as if they have not started to execute at all (rollback).
事务是一种机制,它允许您标记一组操作并以这样的方式执行它们,即它们要么全部执行(提交),要么系统状态就好像它们根本没有开始执行(回滚)。
beginTransaction;
accountB += 100;
accountA -= 100;
commitTransaction;
will either transfer 100 bucks or leave both accounts in the initial state.
将转移 100 美元或将两个帐户都保留在初始状态。
回答by Rad
"A series of data manipulation statements that must either fully complete or fully fail, leaving the database in a consistent state"
“必须完全完成或完全失败的一系列数据操作语句,使数据库处于一致状态”
回答by Mourad BENKDOUR
A transaction is a sequence of one or more SQL operations that are treated as a unit.
事务是作为一个单元处理的一个或多个 SQL 操作的序列。
Specifically, each transaction appears to run in isolation, and furthermore, if the system fails, each transaction is either executed in its entirety or not all.
具体来说,每个事务似乎都是孤立运行的,而且,如果系统出现故障,每个事务要么全部执行,要么全部执行。
The concept of transactions is motivated by two completely independent concerns. One has to do with concurrent access to the database by multiple clients, and the other has to do with having a system that is resilient to system failures.
交易的概念是由两个完全独立的关注点驱动的。一个与多个客户端对数据库的并发访问有关,另一个与拥有一个对系统故障具有弹性的系统有关。
Transaction supports what is known as the ACID properties:
事务支持所谓的 ACID 属性:
- A: Atomicity;
- C: Consistency;
- I: Isolation;
- D: Durability.
- A:原子性;
- C:一致性;
- I:隔离;
- D:耐久性。
回答by Stephen Denne
http://en.wikipedia.org/wiki/Database_transaction
http://en.wikipedia.org/wiki/ACID
ACID = Atomicity, Consistency, Isolation, Durability
http://en.wikipedia.org/wiki/Database_transaction
http://en.wikipedia.org/wiki/ACID
ACID =甲tomicity,Çonsistency,我溶胶化,durability
When you wish for multiple transactional resources to be involved in a single transaction, you will need to use something like a two-phase commitsolution. XAis quite widely supported.
回答by Stephen Denne
In addition to the above responses, it should be noted that there is, at least in theory, no restriction whatsoever as to what kind of resources are involved in a transaction.
除了上面的回答,需要注意的是,至少在理论上,对于交易中涉及什么样的资源没有任何限制。
Most of the time, it is just a database, or multiple distinct databases, but it is also conceivable that a printer takes part in a transaction, and can cause that transaction to fail, say in the event of a paper jam.
大多数情况下,它只是一个数据库或多个不同的数据库,但也可以想象打印机参与了交易,并可能导致交易失败,比如在卡纸的情况下。
回答by Adam Cooper
I would suggest that a definition of 'transaction processing' would be more useful, as it covers transactions as a concept in computer science.
我认为“事务处理”的定义会更有用,因为它将事务作为计算机科学中的一个概念涵盖在内。
From wikipedia:
来自维基百科:
In computer science, transaction processing is information processing that is divided into individual, indivisible operations, called transactions. Each transaction must succeed or fail as a complete unit; it cannot remain in an intermediate state.
在计算机科学中,事务处理是将信息处理分为单独的、不可分割的操作,称为事务。每笔交易必须作为一个完整的单元成功或失败;它不能停留在中间状态。
http://en.wikipedia.org/wiki/Transaction_processing#Implementations
http://en.wikipedia.org/wiki/Transaction_processing#Implementations
回答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.
事务可以定义为被视为最小处理单元的任务集合。每个最小处理单元不能进一步划分。
The main operation of a transaction are read and write.
一个事务的主要操作是读和写。
All transaction must contain four properties that commonly known as ACID properties for the purpose of ensuring accuracy , completeness and data integrity.
所有事务必须包含四个属性,通常称为 ACID 属性,以确保准确性、完整性和数据完整性。
回答by Mohamed Seif
Transaction is an indivisible unit of data processing -All transactions must have the ACID properties:
事务是不可分割的数据处理单元 - 所有事务都必须具有 ACID 属性:
ie:Atomicity,Consistency,Isolation and Durable Transaction is all or nothing but not intermidiate (it means if you transfer your money from one account to another account,one account have to lose that much and other one have to gain that amount,but if you transfer money from one account and another account is still empty that will be not a transaction)
即:原子性、一致性、隔离性和持久性交易是全部或全部,但不是中间的(这意味着如果您将资金从一个帐户转移到另一个帐户,一个帐户必须损失那么多,另一个帐户必须获得该数额,但是如果你从一个账户转账,而另一个账户仍然是空的,这不会是一笔交易)