简体   繁体   English

交易提交?

[英]transaction commit?

Can someone please explain when and why use commit? 有人可以解释何时以及为什么使用commit吗? http://msdn.microsoft.com/en-us/library/ms190295.aspx http://msdn.microsoft.com/en-us/library/ms190295.aspx

.

1) Update in tableX recordM 1)更新tableX recordM

2) Update in tableY recordN 2)在表Y记录中更新N

3) Update in tableZ recordO 3)更新tableZ记录O

4) use commit here 4)在这里使用提交

.. ..

In the case where 2) fails and recordN is not updated, then roll back and do not proceed to 3) and 4) commit? 如果2)失败并且recordN没有更新,则回滚而不进行3)和4)提交? is that where you would use it? 那是你会用它的地方吗?

use commit whenver one or several operation (transaction) are finished, and you wish to make change the data base. 每当一个或多个操作(事务)完成并且您希望更改数据库时,请使用commit。

in your example, if 2) failed, you should do rollback and them the operation 1) will undo, so that will keep your database consistent 在您的示例中,如果2)失败,则应进行回滚,并且操作1)将撤消操作,以便使数据库保持一致

eg. 例如。 if you have a bookshop system, whenever you sell a book you have to do several operations: 如果您有书店系统,则每当出售书籍时,都必须执行以下操作:

    • discount the stock 打折股票
    • create a sale history 创建销售历史
    • charge the client's card 收取客户卡

you will want either all operations done with success or nothing be done. 您要么希望所有操作成功完成,要么什么都不做。 so you should: 所以你应该:

  1. begin transation 开始翻译
  2. do step 1,2,3 执行步骤1,2,3
  3. commit 承诺

if something wrong, rollback(undo) all 如果有问题,请全部回滚(撤消)

Use commit always when you want to treat couple operations as one action. 当您要将对偶操作视为一项操作时,请始终使用提交。 This mean that when at least one fail all are reverted to state before commit. 这意味着当至少一个失败时,所有错误都将恢复为提交前的状态。

It is very useful because some actions should be treated as atom operations eg. 这非常有用,因为某些动作应视为原子操作,例如。 bank operations. 银行业务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM