Transactions in SQL Server ensure data consistency and integrity by grouping multiple operations into a single unit of work. SQL Server provides different transaction modes to control how transactions are handled.
SQL Server supports three transaction modes:
- Autocommit Transactions (Default)
 - Explicit Transactions
 - Implicit Transactions
 - Batch-Scoped Transactions
     Mode
 - Distributed Transactions
 
Types of Transaction Modes in SQL Server
| 
    Transaction
   Mode  | 
   
    Description  | 
   
    Use
   Case  | 
  
| 
   Autocommit (Default Mode)  | 
  
   Every statement is treated as an individual transaction
  and is automatically committed.  | 
  
   General use case for single operations.  | 
 
| 
   Implicit Transactions  | 
  
   A new transaction starts automatically after the previous
  one completes, requiring an explicit   | 
  
   When you want every statement to be part of a transaction.  | 
 
| 
   Explicit Transactions  | 
  
   Transactions are manually controlled using   | 
  
   When you need full control over transactions.  | 
 
| 
   Batch-scoped Transactions  | 
  
   Transactions exist only within a batch of T-SQL
  statements.  | 
  
   Used in scenarios where transactions span multiple queries
  within a batch.  | 
 
| 
   Distributed Transactions  | 
  
   Transactions span across multiple databases or servers.
  Uses Microsoft Distributed Transaction Coordinator (MSDTC).  | 
  
   When working with linked servers or multiple databases.  | 
 
Each mode determines
how transactions are started, managed, and committed or rolled back. Below is a
detailed explanation of each mode, along with examples and test cases.
1. Autocommit Transactions
- Description:
     Every individual SQL statement is treated as a transaction. If the
     statement succeeds, it is automatically committed; if it fails, it is
     rolled back.
 - Use Case:
     Default behavior; suitable for simple, single-statement operations.
 - Example:
 
-- Autocommit mode (default)
INSERT INTO Employees (EmployeeID, Name) VALUES (1, 'John Doe');
-- If the INSERT succeeds, it is
automatically committed.
- Test
     Case:
 
-- Create a table
CREATE TABLE Employees (EmployeeID INT PRIMARY KEY, Name NVARCHAR(100));
-- Insert a row (autocommit)
INSERT INTO Employees (EmployeeID, Name) VALUES (1, 'John Doe');
-- Verify the row is committed
SELECT * FROM Employees; -- Should return the inserted row
2. Explicit Transactions
- Description:
     Transactions are explicitly started using BEGIN
     TRANSACTION and must be explicitly
     ended with COMMIT or ROLLBACK.
 - Use Case:
     Used when multiple operations need to be treated as a single atomic unit.
 - Example:
 
BEGIN TRANSACTION;
INSERT INTO Employees (EmployeeID, Name) VALUES (2, 'Jane Doe');
UPDATE Employees SET Name = 'Jane Smith' WHERE
EmployeeID = 2;
COMMIT; -- Commit the transaction
- Test
     Case:
 
-- Start an explicit transaction
BEGIN TRANSACTION;
-- Insert a row
INSERT INTO Employees (EmployeeID, Name) VALUES (2, 'Jane Doe');
-- Update the row
UPDATE Employees SET Name = 'Jane Smith' WHERE
EmployeeID = 2;
-- Commit the transaction
COMMIT;
-- Verify the changes
SELECT * FROM Employees WHERE
EmployeeID = 2; -- Should return 'Jane Smith'
3.Implicit Transactions
- Description:
     Transactions are implicitly started when a statement is executed and must
     be explicitly ended with COMMIT or ROLLBACK. Enabled using SET
     IMPLICIT_TRANSACTIONS ON.
 - Use Case:
     Useful when you want to ensure that every statement is part of a
     transaction but don’t want to explicitly start a transaction.
 - Example:
 
SET IMPLICIT_TRANSACTIONS ON;
INSERT INTO Employees (EmployeeID, Name) VALUES (3, 'Alice');
UPDATE Employees SET Name = 'Alice Johnson' WHERE
EmployeeID = 3;
COMMIT; -- Commit the transaction
- Test
     Case:
 
-- Enable implicit transactions
SET IMPLICIT_TRANSACTIONS ON;
-- Insert a row (starts a
transaction implicitly)
INSERT INTO Employees (EmployeeID, Name) VALUES (3, 'Alice');
-- Update the row
UPDATE Employees SET Name = 'Alice Johnson' WHERE
EmployeeID = 3;
-- Commit the transaction
COMMIT;
-- Verify the changes
SELECT * FROM Employees WHERE
EmployeeID = 3; -- Should return 'Alice Johnson'
-- Disable implicit transactions
SET IMPLICIT_TRANSACTIONS OFF;
4. Batch-Scoped Transactions Mode
- Transactions exist only within
     the scope of a batch.
 - Used when executing multiple queries within
     a script.
 
Test Case: Batch-Scoped Transactions
BEGIN TRANSACTION;
UPDATE Products SET Price = 800 WHERE ProductID = 1;
COMMIT;
GO  -- Marks the
end of the batch
BEGIN TRANSACTION;
UPDATE Products SET Price = 900 WHERE ProductID = 1;
ROLLBACK;
GO  -- Another
batch, previous transaction state is lost
SELECT * FROM Products WHERE
ProductID = 1;  -- Should show price as 800, since second update was rolled back
Result: The second transaction is rolled back, but the first commit remains.
5. Distributed Transactions Mode
- Used when multiple
     databases or servers are involved.
 - Requires MSDTC (Microsoft Distributed Transaction
     Coordinator).
 - Uses 
BEGIN DISTRIBUTED TRANSACTION. 
Test Case: Distributed Transactions
BEGIN DISTRIBUTED TRANSACTION;
UPDATE Server1.DB1.dbo.Products SET Price = 1000 WHERE ProductID = 1;
UPDATE Server2.DB2.dbo.Products SET Price = 1000 WHERE ProductID = 1;
COMMIT;  -- Ensures both updates succeed together
Result: Both updates commit
together or roll back together,
ensuring data consistency across servers.
Comparison of Transaction Modes
| 
    Mode  | 
   
    Description  | 
   
    When to Use  | 
  
| 
   Autocommit  | 
  
   Every statement is a
  transaction; automatically committed or rolled back.  | 
  
   Default behavior;
  suitable for single-statement operations.  | 
 
| 
   Explicit  | 
  
   Transactions are
  explicitly started and ended using BEGIN, COMMIT, ROLLBACK.  | 
  
   For multi-statement
  operations requiring atomicity.  | 
 
| 
   Implicit  | 
  
   Transactions are
  implicitly started and must be explicitly committed or rolled back.  | 
  
   When you want every
  statement to be part of a transaction without explicit BEGIN.  | 
 
Test Cases for All Modes
Test Case 1: Autocommit
-- Autocommit mode (default)
INSERT INTO Employees (EmployeeID, Name) VALUES (4, 'Bob');
-- Verify the row is committed
SELECT * FROM Employees WHERE
EmployeeID = 4; -- Should return 'Bob'
Test Case 2: Explicit Transaction
-- Explicit transaction
BEGIN TRANSACTION;
INSERT INTO Employees (EmployeeID, Name) VALUES (5, 'Charlie');
UPDATE Employees SET Name = 'Charlie Brown' WHERE
EmployeeID = 5;
COMMIT;
-- Verify the changes
SELECT * FROM Employees WHERE
EmployeeID = 5; -- Should return 'Charlie Brown'
Test Case 3: Implicit Transaction
-- Enable implicit transactions
SET IMPLICIT_TRANSACTIONS ON;
-- Insert a row (starts a
transaction implicitly)
INSERT INTO Employees (EmployeeID, Name) VALUES (6, 'David');
-- Update the row
UPDATE Employees SET Name = 'David Smith' WHERE
EmployeeID = 6;
-- Commit the transaction
COMMIT;
-- Verify the changes
SELECT * FROM Employees WHERE
EmployeeID = 6; -- Should return 'David Smith'
-- Disable implicit transactions
SET IMPLICIT_TRANSACTIONS OFF;
Key Takeaways
- Autocommit:
     Simplest mode; no manual transaction management.
 - Explicit:
     Full control over transactions; ideal for atomic operations.
 - Implicit:
     Automatically starts transactions; requires explicit commit/rollback.
 
When
to Use Each Transaction Mode
| 
    Scenario  | 
   
    Recommended
   Mode  | 
  
| 
   Simple updates, inserts, and deletes  | 
  
   Autocommit (Default Mode)  | 
 
| 
   Ensuring explicit control over transactions  | 
  
   Explicit Transactions  | 
 
| 
   Automatically starting a new transaction after the last
  one  | 
  
   Implicit Transactions  | 
 
| 
   Executing multiple queries as a batch  | 
  
   Batch-Scoped Transactions  | 
 
| 
   Working with multiple databases or linked servers  | 
  
   Distributed Transactions  | 
 
Choose the transaction mode based on your application’s requirements for control and atomicity.
No comments:
Post a Comment