Skip to content

Commit 9778a9c

Browse files
committed
Replace DBCC commands with DMV queries in TID locking sample
1 parent 7a3e49a commit 9778a9c

1 file changed

Lines changed: 46 additions & 48 deletions

File tree

  • samples/features/optimized-locking/transaction-id-locking

samples/features/optimized-locking/transaction-id-locking/README.md

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Every transaction that modifies a row tags that row with its own TID, so each ro
3939
<a name=about-this-sample></a>
4040
## About this sample
4141

42-
- **Applies to:** SQL Server 2025 (or higher)
42+
- **Applies to:** SQL Server 2025 (or higher), Azure SQL Database
4343
- **Key features:** Optimized Locking
4444
- **Workload:** No workload related to this sample
4545
- **Programming Language:** T-SQL
@@ -52,7 +52,7 @@ To run this sample, you need the following prerequisites.
5252

5353
**Software prerequisites:**
5454

55-
1. SQL Server 2025 (or higher)
55+
1. SQL Server 2025 (or higher) or Azure SQL Database
5656

5757
<a name=run-this-sample></a>
5858
## Run this sample
@@ -67,8 +67,6 @@ To run this sample, you need the following prerequisites.
6767
<a name=sample-details></a>
6868
## Sample Details
6969

70-
Currently, the only way to read the TID of a row is by using the `DBCC PAGE` command.
71-
7270
Let's consider the table dbo.TelemetryPacket, with the schema defined in the following T-SQL code snippet.
7371

7472
```sql
@@ -87,74 +85,74 @@ The table schema is designed so that each row occupies exactly one data page.
8785

8886
Insert three rows with default values into the dbo.TelemetryPacket table. Note that this is done in a single transaction.
8987

88+
Before committing the transaction, we query the [sys.dm_tran_locks](https://learn.microsoft.com/sql/relational-databases/system-dynamic-management-views/sys-dm-tran-locks-transact-sql) DMV, which exposes the TID locks as a new resource type = `XACT`.
89+
9090
```sql
91-
BEGIN TRANSACTION
91+
BEGIN TRANSACTION;
92+
9293
INSERT INTO dbo.TelemetryPacket DEFAULT VALUES;
9394
INSERT INTO dbo.TelemetryPacket DEFAULT VALUES;
9495
INSERT INTO dbo.TelemetryPacket DEFAULT VALUES;
95-
COMMIT
96-
```
97-
98-
Let's explore the content of the dbo.TelemetryPacket table, enriched with the PageId column, which shows the result of the undocumented function sys.fn_PhysLocFormatter. Use this function to correlate the rows returned by the `SELECT` with their physical location on disk.
99-
100-
```sql
101-
USE [OptimizedLocking]
102-
GO
10396

10497
SELECT
105-
*
106-
,PageId = sys.fn_PhysLocFormatter(%%physloc%%)
98+
l.resource_description
99+
,l.resource_associated_entity_id
100+
,l.resource_lock_partition
101+
,l.request_mode
102+
,l.request_type
103+
,l.request_status
104+
,l.request_owner_type
107105
FROM
108-
dbo.TelemetryPacket;
109-
```
106+
sys.dm_tran_locks AS l
107+
WHERE
108+
(l.request_session_id = @@SPID)
109+
AND (l.resource_type = 'XACT');
110110

111-
The output is similar to the following, except for the values in the PageId column.
112-
113-
| PageId | PacketID | Device |
114-
| ----------- | --------- | --------- |
115-
| (1:2456:0) | 1 | Something |
116-
| (1:2457:0) | 2 | Something |
117-
| (1:2458:0) | 3 | Something |
111+
COMMIT;
112+
```
118113

119-
Each value in the PageId column follows the format **(FileID:PageID:SlotID)** and represents the physical location of the data.
114+
The resource_description column, in this example, reports the `XACT` value equal to `10:1147:0`.
120115

121-
Let's examine the row where PacketID equals 1. The value (1:2456:0) is composed of three parts separated by ":". Here is what each part represents:
122-
- **1** - the numeric identifier of the database file (FileID)
123-
- **2456** - the page number within the file (PageID)
124-
- **0** - the slot number on the page (SlotID)
116+
TID `1147` represents the identifier of the transaction that inserted the rows and it will be stored in the row data page if the transaction is confirmed. Every subsequent change to the rows will update the TID.
125117

126-
Use the `DBCC PAGE` command to inspect the TID of page 2456.
118+
Now let's make a change on the row identified by the PacketID value 2 and before confirming the transaction let's repeat again the query on the DMV.
127119

128120
```sql
129-
-- Enable trace flag for DBCC PAGE output
130-
DBCC TRACEON(3604);
131-
GO
121+
BEGIN TRANSACTION;
132122

133-
DBCC PAGE ('OptimizedLocking', 1, 2456, 3);
134-
```
123+
UPDATE
124+
t
125+
SET
126+
t.Device = 'Something updated'
127+
FROM
128+
dbo.TelemetryPacket AS t
129+
WHERE
130+
t.PacketID = 2;
135131

136-
The value of the unique TID that modified the row with PacketID equal to 1 is in the **Version Information** section, under the **Transaction Timestamp** attribute, as shown in the following sample data.
132+
SELECT
133+
l.resource_description
134+
,l.resource_associated_entity_id
135+
,l.resource_lock_partition
136+
,l.request_mode
137+
,l.request_type
138+
,l.request_status
139+
,l.request_owner_type
140+
FROM
141+
sys.dm_tran_locks AS l
142+
WHERE
143+
(l.request_session_id = @@SPID)
144+
AND (l.resource_type = 'XACT');
137145

138-
```sql
139-
Version Information =
140-
Transaction Timestamp: 985
141-
Version Pointer: Null
142-
143-
Slot 0 Column 1 Offset 0x4 Length 4 Length (physical) 4
144-
PacketID = 1
145-
Slot 0 Column 2 Offset 0x8 Length 8000 Length (physical) 8000
146-
Device = Something…
146+
COMMIT;
147147
```
148148

149-
TID 985 represents the identifier of the transaction that inserted the rows; every subsequent change to the table rows will update the TID.
149+
Even for the `UPDATE` command, the resource_description column displays the TID of the transaction that is modifying the row. If the transaction is confirmed, the TID will be stored in the data page of the row itself.
150150

151151
<a name=disclaimers></a>
152152
## Disclaimers
153153

154154
The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this sample.
155155

156-
> **Note:** The `DBCC PAGE` command is undocumented and intended for troubleshooting and diagnostic purposes only. It should not be used in production environments without proper understanding and testing.
157-
158156
<a name=related-links></a>
159157
## Related Links
160158

0 commit comments

Comments
 (0)