Transactions and concurrency
ACID and transaction boundaries
Treat related writes as one unit while distinguishing ACID properties.
Lesson 67 of 84
Reading: about 2 min
Lesson + practice: about 15 min
Difficulty: Advanced
On this page4 sections
This lesson uses DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Treat related writes as one unit while distinguishing ACID properties. Run changing examples only on a disposable training instance. The example uses a session-local object or rolls back its changes.
Run the example
CREATE TABLE #Pair (ID int NOT NULL PRIMARY KEY);
BEGIN TRANSACTION;
INSERT INTO #Pair VALUES (1);
INSERT INTO #Pair VALUES (2);
COMMIT TRANSACTION;
SELECT ID FROM #Pair ORDER BY ID;
DROP TABLE #Pair;Read it step by step
- Both inserts share one transaction boundary and are committed together.
- Atomicity means the unit either commits or rolls back. Consistency means a successful transaction preserves the database rules; the primary key enforces one rule, while business rules may need additional logic.
- Isolation governs concurrent visibility, and durability concerns committed data on persistent storage; this temp-table demo does not test crash recovery.
Check the result
| ID |
|---|
| 1 |
| 2 |
The two rows are visible after commit within this session-local temp table.
Practice
Insert IDs 3 and 4 into a temp table inside one transaction, commit, and display both.
Check your answer and expected result
Reveal answer
CREATE TABLE #Pair (ID int NOT NULL PRIMARY KEY);
BEGIN TRANSACTION;
INSERT INTO #Pair VALUES (3);
INSERT INTO #Pair VALUES (4);
COMMIT TRANSACTION;
SELECT ID FROM #Pair ORDER BY ID;
DROP TABLE #Pair;| ID |
|---|
| 3 |
| 4 |
Both rows are present after the single commit.
Your progress
Reveal the check answer or reach the end to complete this lesson.