简体   繁体   中英

transaction commit?

Can someone please explain when and why use commit? http://msdn.microsoft.com/en-us/library/ms190295.aspx

.

1) Update in tableX recordM

2) Update in tableY recordN

3) Update in tableZ recordO

4) use commit here

..

In the case where 2) fails and recordN is not updated, then roll back and do not proceed to 3) and 4) commit? 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.

in your example, if 2) failed, you should do rollback and them the operation 1) will undo, so that will keep your database consistent

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
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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