What is transaction?

Source: Internet
Author: User

What is a transaction? These are database-specific terms. The lazy explained here: it is to treat multiple things as one thing. That is to say, if everyone is on the same ship, they must live together and over together!

Why should I use transactions? Here is another vulgar example:

I am saving money in the bank, so there are several steps:
1. Hand over the money to the staff; 2. Fill in the work order; 3. Sign the form; 4. Confirm and enter the computer.

If I had handed over the money to the staff, I would have signed 3. The buddy suddenly suffered a heart attack and fell over. Then, my money had not been input into the computer, but I paid the money and signed it for confirmation. There was no other record. Wouldn't I lose my money ??? My hard-earned money! Return it to me !!

As a result, the database produces the term Transaction, that is, either success or failure, and restoring the original state.
  
Or write the program:

Create Proc sp I deposit (@ M Money, @ iOperator Int)
As
Begin
Declare @ I int

Begin Tran -- activate a transaction
Exec @ I = sp pay @ m, @ iOperator
If @ I <> 0 -- the system Error code @ Error is generally used here. I will not use it here for example. Depends on the actual situation.
Begin
Rollback Tran -- roll back the transaction
RaisError! :( ', 16, 1) with Log -- Record Log
Return-1 -- Return Error Code
End

Exec @ I = sp fill in @ m, @ iOperator
If @ I <> 0
Begin
Rollback Tran -- roll back the transaction
RaisError ?? ', 16, 1) with Log
Return-2
End

Exec @ I = sp signature @ m
If @ I <> 0
Begin
Rollback Tran -- roll back the transaction
RaisError ('depends on me? What bad banks do? I can't write anything for 3 transactions !! I am not saving it !! Do not sign! ', 16, 1) with Log
Return-3
End

Exec @ I = sp enter the computer @ m, @ iOperator
If @ I <> 0
Begin
Rollback Tran -- roll back the transaction
RaisError ('what does it mean? Is the disk space full? Okay, give me the money, and I will go to the bank next to it! ', 16, 1) with Log
Return-4
End
 
Commit Tran -- submit a transaction
Return 0
End
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.