30.06.2013 Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

Performance Tuning with SQL Server Dynamic Management Views

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Transactions<br />

BEGIN TRANSACTION<br />

SELECT DTST.[session_id] ,<br />

DTST.[transaction_id] ,<br />

DTST.[is_user_transaction]<br />

FROM sys.[dm_tran_session_transactions] AS DTST<br />

WHERE DTST.[session_id] = @@SPID<br />

ORDER BY DTST.[transaction_id]<br />

COMMIT<br />

session_id transaction_id is_user_transaction<br />

----------- -------------------- -------------------<br />

56 1550381 1<br />

(1 row(s) affected)<br />

Listing 4.10: Basic query against sys.dm_tran_session_transactions for transactions<br />

on the current session.<br />

sys.dm_tran_active_transactions<br />

The sys.dm_tran_active_transactions DMV returns a list of all transactions<br />

that are active at the time the query is executed. This view operates at the scope of the<br />

entire <strong>SQL</strong> instance, meaning that results are returned for all databases on the instance.<br />

Since this view provides a point-in-time snapshot of currently active transactions, the<br />

results will change each time the query is executed, depending upon the state of the<br />

individual transactions.<br />

Identification columns returned by this view are transaction_id, which uniquely<br />

identifies a transaction across the entire <strong>SQL</strong> <strong>Server</strong> instance, and name, which is the<br />

name of the transaction. I consider it a good habit, if not a best practice, to name<br />

transactions, as the following code snippet demonstrates:<br />

BEGIN TRANSACTION Trans_Name<br />

--Some <strong>SQL</strong> Code goes here<br />

COMMIT TRANSACTION Trans_Name<br />

140

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!