What is a database transaction?

前端 未结 11 2264
耶瑟儿~
耶瑟儿~ 2020-11-27 09:34

Can someone provide a straightforward (but not simpler than possible) explanation of a transaction as applied to computing (even if copied from Wikipedia)?

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 10:16

    Here's a simple explanation. You need to transfer 100 bucks from account A to account B. You can either do:

    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.

    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.

提交回复
热议问题