How to Decide to use Database Transactions

后端 未结 6 2278
小鲜肉
小鲜肉 2020-12-11 15:10

How do you guys decide that you should be wrapping the sql in a transaction?

Please throw some light on this.

Cheers !!

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 15:44

    A transaction should be used when you need a set of changes to be processed completely to consider the operation complete and valid. In other words, if only a portion executes successfully, will that result in incomplete or invalid data being stored in your database?

    For example, if you have an insert followed by an update, what happens if the insert succeeds and the update fails? If that would result in incomplete data (in this case, an orphaned record), you should wrap the two statements in a transaction to get them to complete as a "set".

提交回复
热议问题