liveBook · Manning

Database transaction

As explained in 100 SQL Server Mistakes and How to Avoid Them

A database transaction is a logical unit of work whose operations are committed or rolled back as a group.

A database transaction groups one or more data-changing T-SQL statements into one logical unit: all changes commit together, or all are undone with a rollback. Its core ACID properties are atomicity, consistency, isolation, and durability. section 7.1

Mistake #26: assuming an error automatically rolls back a transaction. A stored procedure can fail after making an earlier change, leaving an open transaction behind. Check SELECT @@TRANCOUNT; when no transaction should remain open, it should be 0. Roll back the outstanding transaction before continuing. section 7.1

Sales order insert failure

An order procedure inserts a header and a detail row inside a transaction. Renaming the detail table makes the detail insert fail. SQL Server reports both the invalid object and a mismatched transaction count, because the transaction remains open rather than being rolled back automatically. section 7.1 section 7.1

Listing 7.1 Creating the dbo.InsertSalesOrder stored procedure
CREATE PROCEDURE dbo.InsertSalesOrder
   @SalesOrderNumber NVARCHAR(12),
   @SalesOrderDate DATE,
   @SalesPersonID INT,
   @SalesAreaID INT,
   @CustomerID INT,
   @SalesOrderDeliveryDueDate DATE,
   @SalesOrderDeliveryActualDate DATE,
   @CurrierUsedForDelivery NVARCHAR(32),
   @ProductID INT,
   @Quantity INT
AS
BEGIN
   BEGIN TRANSACTION
 
       DECLARE @CustomerPrefix NVARCHAR(12) ;
       SET @CustomerPrefix = (
           SELECT SUBSTRING(CustomerCompanyName,1,3)
           FROM dbo.Customers
           WHERE CustomerID = @CustomerID
       ) ;
       
       INSERT INTO dbo.SalesOrderHeaders
       VALUES (
             @CustomerPrefix + @SalesOrderNumber
           , @SalesOrderDate
           , @SalesPersonID
           , @SalesAreaID
           , @CustomerID
           , @SalesOrderDeliveryDueDate
           , @SalesOrderDeliveryActualDate
           , @CurrierUsedForDelivery
       ) ;
 
       INSERT INTO dbo.SalesOrderDetails (
             ProductID
           , Quantity
           , SalesOrderNumber
       )
       VALUES (
             @ProductID
           , @Quantity
           , @CustomerPrefix + @SalesOrderNumber
       ) ;
 
   COMMIT
END

Inspect the order procedure to see a transaction wrapped around related header and detail inserts—the kind of work that must succeed or be undone as a group. section 7.1

Use transactions defensively:

  • Put related updates that must stand or fall together in the same transaction. section 7.1
  • Handle failures explicitly with TRY...CATCH and rollback logic rather than relying on an automatic rollback. section 7.1
  • Avoid deleting a large number of rows in one transaction; split the work into batches to reduce the risk of filling the transaction log. section 5.9

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Defines ACID properties and demonstrates open-transaction diagnosis, rollback, TRY...CATCH, and transaction-log consequences.
Examples
Uses an InsertSalesOrder procedure, a renamed detail table, constraint failures, error messages, and batched deletion guidance.
What it assumes
Introduces transactions from a T-SQL stored-procedure scenario without assuming deep SQL Server expertise.
What you can do after
Diagnose/debug with it by detecting an open transaction and rolling it back after a failed procedure.
How it connects
Connects transactions to ACID, TRY...CATCH, ROLLBACK, @@TRANCOUNT, XACT_ABORT, stored procedures, and the transaction log.

Also explained in

Compare how 14 books explain database transaction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.