What are transactions?

Microsoft defines transactions as:
“A transaction is a sequence of operations performed as a single logical unit of work. A logical unit of work must exhibit four properties, called the atomicity, consistency, isolation, and durability (ACID) properties, to qualify as a transaction.
A transaction must be an atomic unit of work; either all of its data modifications are performed, or none of them is performed.
When completed, a transaction must leave all data in a consistent state. In a relational database, all rules must be applied to the transaction’s modifications to maintain all data integrity. All internal data structures, such as B-tree indexes or doubly-linked lists, must be correct at the end of the transaction.
Modifications made by concurrent transactions must be isolated from the modifications made by any other concurrent transactions. A transaction either recognizes data in the state it was in before another concurrent transaction modified it, or it recognizes the data after the second transaction has completed, but it does not recognize an intermediate state. This is referred to as serializability because it results in the ability to reload the starting data and replay a series of transactions to end up with the data in the same state it was in after the original transactions were performed.
After a transaction has completed, its effects are permanently in place in the system. The modifications persist even in the event of a system failure.”

Sounds great! Sign me up! Well there’s a drawback to transactions, they cause locking. Depending on how many records you change, whole tables could get locked.

How a lock is applied to an object is complicated – there are row locks, Page Locks and Table locks. SQL internally has rules around when to escalate from row locks  Page Locks  table locks

When an object is locked, nothing else can change that object.

So the long and the short of the matter is – use transactions sparingly – be really sure you need them and keep them short.

Transactions can cause log growth – careful you don’t run out of disk space! Transactions can take a long time to rollback.

Transactions are case-sensitive

Begin tran x
Commit X

Is not the same as:

Begin tran X
Commit X