Transactions Locks «Prev  Next»

Lesson 2What is a SQL-Server transaction?
ObjectiveDefine Transactions and how they are used in SQL Server

Define Transactions and how they are used in SQL Server

Transactions in SQL Server 2022, as in previous versions, are a mechanism to group multiple SQL statements into a single unit of work. They ensure that either all the statements within the transaction are successfully executed, or, if any of them fails, the entire transaction is rolled back to maintain data consistency. This prevents partial updates, which could lead to data corruption or loss. In SQL Server 2022, transactions can be managed in three modes:
  1. Autocommit Transactions: This is the default mode where every T-SQL statement is treated as a transaction and is committed or rolled back based on its success.
  2. Implicit Transactions: In this mode, you explicitly start a transaction using the BEGIN TRANSACTION command, and SQL Server implicitly starts a new transaction for every DML statement. You must explicitly commit or rollback the transaction using the COMMIT or ROLLBACK command.
  3. Explicit Transactions: These are transactions that are explicitly defined and managed by the user. The user starts the transaction using BEGIN TRANSACTION, and it remains open until it is explicitly committed or rolled back.

The key properties of transactions in SQL Server, including in the 2022 version, are:
  1. Atomicity: All statements within a transaction are treated as a single unit, ensuring that either all succeed or the entire transaction is rolled back.
  2. Consistency: Transactions ensure that the database remains in a consistent state by only committing changes if all statements within the transaction are successful.
  3. Isolation: Transactions allow multiple users to access the same data concurrently without interfering with each other's work.
  4. Durability: Once a transaction is committed, the changes are made permanent and are not lost even in the event of a system failure.

SQL Server 2022 also introduces several enhancements to its transaction management capabilities, such as improvements in performance and the introduction of new features for managing and monitoring transactions. For example, the introduction of Accelerated Database Recovery (ADR) in SQL Server 2022 improves the overall availability and scalability of the database, particularly around persistent version store (PVS) cleanup and management. In conclusion, transactions in SQL Server 2022 play a crucial role in maintaining data integrity and consistency by ensuring that all database modifications are performed as a single unit of work.

Purpose of Transactions

The concept of transactions is very important in the SQL Server environment. A transaction[1] is a unit of work in which all Transact-SQL statements contained within that unit are treated as a whole. Any Transact-SQL statements in a transaction that fails will cause the entire transaction to fail. As a result, transactions protect against a statement modifying data after a previous statement has failed.

Three Concepts Relating to Transactions

It is important to understand the following three concepts relating to transactions:
  1. Begin Transaction: Begins the definition of a unit of work.
  2. Commit Transaction: Ends the definition of a unit of work and applies all Transact-SQL statements that are executed between the Begin Transaction statement and the Commit Transaction statement.
  3. Rollback Transaction: Ends the definition of a unit of work and cancels all Transact-SQL statements that are executed between the Begin Transaction statement and the Rollback Transaction statement.

ACID test for Transactions:

ACID test for Transactions: For a single unit of work to be a transaction, it must pass the ACID test.
ACID stands for Atomicity, Consistency, Isolation, and Durability.
  1. Atomicity: Either every statement is committed or none of them are committed.
  2. Consistency: Data integrity must be enforced to ensure the Transact-SQL statements wrapped within the transaction have all rules applied to them.
  3. Isolation: Concurrent transactions either read the data at the beginning or the end of the transaction, but nowhere in between. This is controlled by isolation level, as discussed in a later lesson in this module.
  4. Durability: When a transaction is completed, the effects on the data are made permanent in the database.

The Transact-SQL statements that are contained between the Begin Transaction and Commit Transaction statements are ‘wrapped in a transaction.’ These are considered to be the complete unit of work.
ACID is a concept that database admins look for when evaluating database and application architectures. A robust database should possess these four attributes. Atomicity is an all-or-none proposition. Consistency guarantees that a transaction never leaves your database in an incomplete state. Isolation keeps transactions separated from each other until they are finished. Durability guarantees that the database will keep track of pending changes in such a way that the server can recover from an abnormal termination. The diagram below summarizes the four components of an ACID transaction.
ACID Components
ACID Components for evaluating database and application architectures

When you design any system or database, make sure you select the database which contains these 4 components as that will help you better develop applications for your business. Historical Note Andreas Reuter and Theo Hoerder created the acronym ACID in the year 1983. SQL Server, Oracle, MySQL, PostgreSQL are some of the databases which follows ACID properties by default.

[1]Transaction: A set of SQL commands that are grouped together and either all succeed or all fail as a unit.

SEMrush Software