事务处理:如何同时管理事务? [英] transaction handling : How are simultaneous transaction managed?

查看:93
本文介绍了事务处理:如何同时管理事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在ABC(资金:1000)帐户上发生了6000次提款交易,并且同时从ATM提款了8000次.然后将如何管理交易.数据库将只打开一个连接,并且不允许新连接到同一帐户吗? ....在这种情况下,当需要同时在同一帐户上完成交易时,会发生什么情况,以及如何管理这些事情.

Suppose a withdrawal transaction of 6000 is happening on account ABC(fund:1000) and at the same time a withdrawl of 8000 is carried out from ATM. Then how will the transaction be managed. Will the database open just one connection and doesnt allow new connection to the same account ?? .... What will happen in such cases when a transaction needs to be done on same account simultaneously and how are these things managed.

推荐答案

这是一个非常复杂的主题的好问题.有一个技术数据库术语 transaction ,它保证两个冲突的操作不能同时发生;这是通过实施ACID原则(原子性,一致性,隔离性,耐久性)来实现的. ACID是关系数据库(例如Oracle,Postgres,MySQL和MSSQL)的核心.非常简短:

This is a good question on a very complex topic. There's a technical database term, transaction, which guarantees that two conflicting operations cannot happen at the same time; this happens by implementing the ACID doctrine (Atomicity, Consistency, Isolation, Durability). ACID is the very core of relational databases (such as Oracle,Postgres,MySQL and MSSQL). Extremely briefly:

  • 原子性表示要么整个交易都发生,要么什么都没有发生:要么从一个地方提取钱,将钱存到另一个地方,然后记录整个事情(然后进行交易成功完成),或者事务中止(回滚),好像什么都没发生;这样可以保证您不会只是没有存款的提款.

  • Atomicity means either the whole transaction happens, or none of it happens: either the money is withdrawn from one place, deposited to another, and the whole thing is logged (and the transaction completes successfully), or the transaction is aborted (rolled back), as if nothing happened; this guarantees you can't have e.g. only a withdrawal without a deposit.

一致性表示您始终处于无错误状态:交易已完成或根本没有发生,因此不会有不完整或不合格的交易(例如,您不能同时更新两次余额,因为状态可能会不一致)

Consistency means that you have an error-free state at all times: transactions either complete or they don't happen at all, so there are no incomplete or botched transactions (e.g. you can't have two updates of the balance at the same time, as you could get an inconsistent state)

隔离意味着发生的任何请求都可以假定没有其他东西在接触数据-例如在您的情况下,ATM不必在乎谁在尝试访问该帐户.这可能意味着执行速度较慢(糟糕,现在无法访问此行,请稍候"),但会大大简化应用程序算法.

Isolation means that any request that happens can assume that nothing else is touching the data - e.g. in your case, the ATM doesn't have to care who else is trying to access the acount. This may mean slower execution ("oops, access to this row is not available now, please wait"), but significantly simplifies the application algorithms.

耐久性意味着即使系统崩溃,这种情况仍然成立-即使断电,状态也始终保持一致.

Durability means that this still holds even if the system crashes - there's always a consistent state, even if power goes out.

例如参见以便进一步阅读: http://www.agiledata.org/essays/transactionControl.html

See e.g. this for further reading: http://www.agiledata.org/essays/transactionControl.html

因此,实际上,不会同时"发生:第一个请求排在第一位,第二个请求排在第二位(由于第一个请求锁定了余额的行,因此第二个过程必须等到它变得可用).由于隔离,您无需担心同时":要么有足够的钱来提取现在,要么没有;或者没有.完全无关紧要的另一笔提款正在排队,并且将在50毫秒内尝试一次(当第一个请求完成时,数据库将删除相关行上的锁,然后队列中的下一个请求将发生).以当前计算机的速度,您可能会感知同时发生这些事件(几乎看不到1/20秒),但实际上它们不是连续发生的.

So, in practice, "simultaneously" doesn't happen: one request will be first in line, and another will be second (as the row with your balance is locked by the first request, the second process has to wait until it becomes available). Due to isolation, there's no need to worry about "at the same time": either there's enough money to withdraw now, or there isn't; it doesn't matter at all that another withdrawal is queued and will be attempted in 50 milliseconds (when the first request completes, database removes the lock on the relevant rows, and next request in the queue will happen). With the speed of current computers, you may perceive those to be happening simultaneously (1/20th of a second is almost imperceptible), but in reality they are not, they are sequential.

这可能也很有趣: http://en.wikipedia.org/wiki/Concurrency_control #Database_transaction_and_the_ACID_rules

这篇关于事务处理:如何同时管理事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆