Coding Effective Transactions
Poorly written or inefficient transactions can have a detrimental effect on concurrency of access to data and overall application performance. SQL Server can hold locks on a number of resources while the transaction is
As you have already seen, transaction information is recorded in each database's transaction log. However, long-running transactions can be a cause of consternation to the system administrator who is attempting to back up and prune the transaction log. Only the inactive portion of the log can be truncated during this operation. The inactive portion of the log is the pages containing log records for all completed transactions prior to the first log record of the oldest still-active transaction (see Figure 31.5). Even if completed transactions follow the first record of the oldest active transaction, they cannot be removed from the log until the oldest active transaction completes. This is because the log is pruned by clearing out entire pages of information prior to the oldest active transaction. Pages after this point cannot be cleared because they might contain records for the active transaction that would be needed in the event of a rollback or database recovery.
Figure 31.5. The inactive portion of the log is the pages in the log prior to the oldest active transaction.
In addition to preventing the log from being pruned, long-running transactions can degrade concurrency by holding locks for an extended period of time, preventing other users from accessing the locked data.
To get information about the oldest active transaction in a database, you can use the
command. The syntax is as
DBCC OPENTRAN [('DatabaseName' DatabaseId)] [WITH TABLERESULTS [, NO_INFOMSGS]]
The following example examines the oldest active transaction for the pubs database:
DBCC OPENTRAN (pubs) go Transaction information for database 'pubs'. Oldest active transaction: SPID (server process ID) : 51 UID (user ID) : 1 Name : t1 LSN : (62:207:1) Start time : Jun 13 2001 11:58:16:820PM DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBCC OPENTRAN returns the spid of the process that initiated the transaction, the user ID, the name of the transaction (this is when naming transactions are helpful because they might help you identify the SQL code that initiated the transaction), the Log Sequence Number (LSN) of the page containing the initial BEGIN TRAN statement for the transaction, and finally, the time the transaction was started.
If you specify the
option, this information is returned in two
CREATE TABLE #opentran_results ( result_label VARCHAR(30), result_value VARCHAR(46)) insert #opentran_results exec ('dbcc opentran (pubs) WITH TABLERESULTS, no_infomsgs') select * from #opentran_results go result_label result_value ------------------------------ ---------------------------------------------- OLDACT_SPID 51 OLDACT_UID 1 OLDACT_NAME t1 OLDACT_LSN (62:211:11) OLDACT_STARTTIME Jun 14 2001 12:50:28:233AM
No active open transactions. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
provides a means for you to identify which transactions are potential problems based on their longevity. If you capture the process information at the same time using
, you can identify who or what application is