Explain Codes LogoExplain Codes Logo

How to find current transaction level?

sql
transaction-isolation
sql-server
database-management
Anton ShumikhinbyAnton Shumikhin·Dec 9, 2024
TLDR

Fetch the transaction depth with @@TRANCOUNT:

SELECT @@TRANCOUNT;

Uncover isolation level using DBCC USEROPTIONS and seek the isolation level row:

DBCC USEROPTIONS;

Get the details using DMVs

DBCC USEROPTIONS gives a quick snapshot, but for advanced scripts or applications, system dynamic management views (DMVs) are handy. Use sys.dm_exec_sessions to determine the transaction isolation level for the current session:

-- In SQL Server, a little bit of isolation can go a long way! SELECT transaction_isolation_level FROM sys.dm_exec_sessions WHERE session_id = @@SPID;

The constant values for isolation levels in the Microsoft documentation are:

  • 0 = Unspecified
  • 1 = ReadUncommitted
  • 2 = ReadCommitted
  • 3 = Repeatable
  • 4 = Serializable
  • 5 = Snapshot

The Concurrency vs Consistency Showdown

Choosing different isolation levels is akin to a balancing act between data consistency and concurrency. Crucial factors include system load and expected transaction conflicts. Understanding isolation levels can shed light on peculiar deadlocks or tardy transactions.

Concurrency under microscopic view

To generalize isolation impact, play around with the levels and compare performance metrics. Higher levels like Serializable ensure data consistency, but often ignite the unwelcome fire of contention. Tailoring scripts to specific scenarios and SQL server versions is an ace up the sleeve for optimal data integrity.

Guide to isolation levels selection

  • READ UNCOMMITTED: Suitable for scenarios where accuracy is non-essential.
  • READ COMMITTED: Strikes a harmonious balance between accuracy and performance.
  • REPEATABLE READ: Comes in handy when intermediate reads must remain consistent.
  • SERIALIZABLE: Ensures complete consistency but might fire up concurrency issues.

Transaction count vs. isolation level

Distinguishing transaction count (@@TRANCOUNT) from isolation level is like separating apples from orangutans. While @@TRANCOUNT indicates the nesting level of active transactions, it doesn't give insight into concurrency controls.

Cross-database Case

In complex environments, checking the isolation level for transactions affecting multiple databases is key. To track cross-database transaction isolation levels, cook up a multi-join query:

-- Because two databases are better than one! SELECT s.transaction_isolation_level, d.name AS 'database' FROM sys.dm_exec_sessions s JOIN sys.databases d ON s.database_id = d.database_id WHERE session_id = @@SPID;

Diagnosing Issues

Diagnosing performance issues, like a detective with a magnifying glass, revolves around the current transaction isolation level. It sheds light on lock contention or deadlocks. Analysing these transactions helps in tailoring the isolation levels or locking hints, maintaining the equilibrium of consistency and availability.