24/09/2025

Introduction to SQL Server Transactions (Transaction Isolation Part 3)

This is third part of "Introduction to SQL Server Transaction" series. You can see previous sections below:

Part 1

Part 2

In this part we discuss how SQL server has implemented concurrency control.

Locking and Versioning

SQL server uses following two techniques to implement concurrency control:

  1. Locking
  2. Versioning

Locking

Locking is the traditional mechanism SQL Server uses to isolate transactions.

When a transaction accesses data, SQL Server places locks on the data to prevent other transactions from making conflicting changes. Locking type and granularity decide the effect of the lock and the scale.

Locking Types

There are different types of locks SQL server can placed. Each lock type has some level of restrictions for other transactions. Here is summary of locking types and what it blocks:


We will have a talk about type of locks and locking in detail in future blogs.

Locking Granularity: Locks can be applied at row level, page level, table level, or even database level. Granularity allows SQL server to not to lock more objects than it required.

When lock is placed on row level, only that row is restricted from accessed by other transactions. Other rows are free to read and write operations, from other transactions. This reduce the blocking

Page level locks on the other hand locks all rows in that page from read or modification (depend on lock type). Same with table and database level locks, they lock more rows, hence more data and prone for more blocking issues.

Locking is primarily used in Read Committed, Repeatable Read, and Serializable isolation levels.

Versioning

Versioning uses a multi-version concurrency control (MVCC) approach. Instead of locking data for readers, SQL Server maintains row versions (multiple version of a row) in tempdb, allowing readers to see a consistent snapshot of the data.

When a row is modified, SQL Server keeps an older version in tempdb so that readers can still access the version valid at the start of their transaction or query.

This is a much recent technique and Isolation levels using versioning are Read Committed Snapshot (RCSI) and Snapshot Isolation (SI).

Versioning helps reduce blocking and deadlocks, improving concurrency.


17/09/2025

Recycling IIS App Pools via Scheduled Task

Don't ask me why (it's complicated), but we had a situation where we wanted to recycle several of our production IIS App Pools on one of our web server.

We want to do this as a Windows Schedule Task, because it was required to be done during very early mornings (daily job, executed once).

Our initial plan was to have a PowerShell script and ran it through Windows Task Scheduler.

So we have asked a script from ChatGPT for that (BTW: who write PowerShell script manually these days?)

This is what was provided:

<#
.SYNOPSIS
    Recycles a specific IIS Application Pool.

.DESCRIPTION
    This script recycles a given IIS Application Pool using the WebAdministration module.
    You must run it with Administrator privileges.

.PARAMETER AppPoolName
    The name of the IIS Application Pool to recycle.

.EXAMPLE
    .\Recycle-AppPool.ps1 -AppPoolName "MyAppPool"
#>

param (
    [Parameter(Mandatory=$true)]
    [string]$AppPoolName
)

# Import IIS module
Import-Module WebAdministration -ErrorAction Stop

# Check if the app pool exists
$appPool = Get-ChildItem IIS:\AppPools | Where-Object { $_.Name -eq $AppPoolName }

if ($null -eq $appPool) {
    Write-Host "Application Pool '$AppPoolName' not found." -ForegroundColor Red
    exit 1
}

try {
    Write-Host "Recycling Application Pool: $AppPoolName ..." -ForegroundColor Cyan
    Restart-WebAppPool -Name $AppPoolName
    Write-Host "Successfully recycled Application Pool: $AppPoolName" -ForegroundColor Green
}
catch {
    Write-Host "Failed to recycle Application Pool '$AppPoolName'. Error: $_" -ForegroundColor Red
    exit 1
}


We could run this following manner:
.\Recycle-AppPool.ps1 -AppPoolName "MyAppPool"

One line for each app pool we want to recycle.

But then, when we asked ChatGPT, to give command line for this to run in Windows Task Scheduler, it has provided this:
powershell.exe
-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\Recycle-AppPool.ps1" -AppPoolName "MyAppPool"

However, proactively, it has provided much simpler approach as well. That was the surprise here.

ChatGPT has suggested to use appcmd.exe.

Never heard that before. Then I realized it is a command line utility which get installed when we install IIS stack.

It was much easier to use it (though it had less error handling compared to PowerShell script).

%windir%\system32\inetsrv\appcmd.exe
recycle apppool /apppool.name:"MyAppPool"

At the end we choose to use appcmd.exe.

That's something we learned, this week. Thanks ChatGPT.



Get Windows Capabilities - Powershell

Recently I came across very useful PowerShell cmdlet to manage windows OS. It is called  Get-WindowsCapability It is part of DISM module (th...