What does a transaction around a single statement do?

后端 未结 6 810
一向
一向 2020-11-27 12:45

I understand how a transaction might be useful for co-ordinating a pair of updates. What I don\'t understand is wrapping single statements in transactions, which is 90% of

6条回答
  •  旧巷少年郎
    2020-11-27 13:21

    As Charles Bretana said, "it does nothing" -- nothing in addition to what is already done.

    Ever hear of the "ACID" requirements of a relational database? That "A" stands for Atomic, meaning that either the statement works in its entirety, or it doesn't--and while the statement is being performed, no other queries can be done on the data affected by that query. BEGIN TRANSACTION / COMMIT "extends" this locking functionality to the work done by multiple statements, but it adds nothing to single statements.

    However, the database transaction log is always written to when a database is modified (insert, update, delete). This is not an option, a fact that tends to irritate people. Yes, there's wierdness with bulk inserts and recovery modes, but it still gets written to.

    I'll name-drop isolation levels here too. Fussing with this will impact individual commands, but doing so will still not make a declared-transaction-wrapped query perform any differently than a "stand-alone" query. (Note that they can be very powerful and very dangeroug with multi-statement declared transactions.) Note also that "nolock" does not apply to inserts/updates/deletes -- those actions always required locks.

提交回复
热议问题