MSDN Documentation

SQL Database Engine - Transactions

Introduction to SQL Transactions

Transactions are a fundamental concept in database management systems. They provide a mechanism to ensure data integrity and consistency, especially in multi-user environments where concurrent access to data is common. A transaction is a sequence of one or more operations performed as a single logical unit of work.

What are Transactions?

In the context of a SQL database, a transaction is a set of SQL statements that are executed together. Either all the statements in the transaction are successfully completed, or none of them are. This "all or nothing" principle is crucial for maintaining the reliability of your data. Consider an e-commerce scenario: transferring funds from one account to another. This involves debiting one account and crediting another. If the debit succeeds but the credit fails, the system would be in an inconsistent state. A transaction ensures that both operations succeed or neither does.

The ACID Properties

Transactions adhere to a set of properties known as ACID, which guarantees their reliability:

Atomicity

Atomicity means that the transaction is treated as a single, indivisible unit. If any part of the transaction fails, the entire transaction is rolled back, and the database is left unchanged as if the transaction never occurred. Conversely, if all parts of the transaction succeed, the changes are permanently recorded.

Consistency

A transaction must bring the database from one valid state to another. It ensures that any data written to the database is valid according to all defined rules, including constraints, cascades, and triggers. If a transaction violates database rules, it will be rolled back.

Isolation

Isolation ensures that concurrent transactions do not interfere with each other. The effect of a transaction should be as if it were the only transaction running on the system. This is often achieved through locking mechanisms and different isolation levels, which we will discuss later.

Durability

Once a transaction has been committed, its changes are permanent and will survive any subsequent system failures, such as power outages or crashes. The database guarantees that committed data is saved to persistent storage.

Transaction States

A transaction can be in several states throughout its lifecycle:

Managing Transactions

SQL provides specific commands to manage transactions:

BEGIN TRANSACTION

This statement marks the beginning of a new transaction. All subsequent SQL statements will be part of this transaction until a COMMIT or ROLLBACK statement is issued. Some database systems use START TRANSACTION as an alias.

BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 1;
UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 2;
-- Potentially more operations here

COMMIT TRANSACTION

This statement signifies the successful completion of a transaction. All changes made within the transaction are made permanent in the database.

COMMIT TRANSACTION;

ROLLBACK TRANSACTION

This statement undoes all changes made within the current transaction since the last BEGIN TRANSACTION. If an error occurs or if you need to abort the transaction for any reason, ROLLBACK is used.

ROLLBACK TRANSACTION;

Savepoints

Savepoints allow you to roll back a transaction to a specific point within its execution, rather than rolling back the entire transaction. This is useful for complex transactions where you might want to undo a portion of the work.

BEGIN TRANSACTION;
UPDATE Products SET Stock = Stock - 5 WHERE ProductID = 101;
SAVEPOINT BeforeUpdatePrice;
UPDATE Products SET Price = Price * 1.1 WHERE ProductID = 101;
-- If something goes wrong here:
-- ROLLBACK TRANSACTION BeforeUpdatePrice;
-- Otherwise, continue and commit:
COMMIT TRANSACTION;

Understanding Isolation Levels

The degree to which one transaction must be isolated from the data modifications made by other concurrent transactions is controlled by the transaction isolation level. Common levels include:

The default isolation level varies between database systems (e.g., SQL Server defaults to READ COMMITTED, PostgreSQL defaults to READ COMMITTED).

Best Practices for Transactions

Properly managing transactions is paramount to building robust and reliable database applications. Always test your transaction logic thoroughly to ensure data integrity under various conditions.