Partilhar via


Monitoring SQL Database Deadlocks

You can set up the system to log deadlocks that occur in the SQL database. The deadlocks are recorded in the Windows Event Log of computer running Microsoft Dynamics NAV Server. The log entries provide information about the C/AL code that was run when the deadlock occurred, along with the deadlock report from SQL Server. This information can help you identify and resolve problem areas in the application design.

About Deadlocks

Deadlocks can prevent users from completing tasks in the Dynamics NAV client. A deadlock occurs when two or more processes or transactions block each other from continuing because each has locked a database resource that the other transaction needs. SQL Server handles deadlocks by terminating and rolling back transactions that were started after the first transaction.

For general information about deadlocks, see Detecting and Ending Deadlocks.

Setting Up Deadlock Logging

Setting up deadlock logging requires you to configure the SQL Server instance and the Microsoft Dynamics NAV Server instance.

Note

If you installed the Microsoft Dynamics NAV Server and database components by using the Install Demo option in the Microsoft Dynamics NAV Setup, then deadlock logging is set up by default.

Configure the SQL Server instance

To configure the SQL Server instance to log deadlocks, you must assign specific permissions to the database login for the service account that is used on the Microsoft Dynamics NAV Server instance. You can do this using SQL Server Management Studio.

In SQL Server Management Studio, connect to the SQL server instance for Dynamics NAV, and then grant the following permissions:

  • On the database level, grant the login the View database state permission.
  • On the SQL server instance level, grant the login both Alter any event session and View server state permissions.

For detailed steps on how to do this, see Giving the account necessary database privileges in SQL Server.

To verify the setup, connect from a client, like the Web client or Windows client. When the session is established with the database, a session for monitoring the Dynamics NAV database appears under Management > Extended Events > Sessions.

Configure where to store deadlock events

By default, SQL Server uses an in-memory data structure called a ring_buffer target to store deadlock events. When the Microsoft Dynamics NAV Server is notified about the deadlock, it reads data from the target ring_buffer target. You have the option to also store the events to a file on the SQL Server, called an event_file target, and configure the Microsoft Dynamics NAV Server to read from this file instead of the ring_buffer target. An important difference between the ring_buffer target and event_file target is that the ring_buffer target has a storage size limitation of 5MB, while the event_file target provides a much greater storage capacity. Using the event_file target can eliminate potential overloads in high volume situations. So, if your setup has a high volume of database traffic, you might have to change the SQL Server to write deadlock events to an event_file target as described the the steps that follow. If you want to use the default ring_buffer target, then no further action is required.

Note

Reading from the event_file target is only supported in Microsoft Dynamics NAV 2018 Cumulative Update 13 and later, and is not supported in Microsoft Dynamics NAV 2017.

  1. Modify the deadlock monitoring session to use a file-based target (known as an event_file target).

    The event_file target writes event session output from a buffer to a disk file that you specify. There are two ways to do this:

    • From Object Explorer, open the session's Properties, and then on the Data Storage page, add an event_file type target.
    • Using a query, run the ALTER EVENT SESSION transact-sql statement. For example:
      ALTER EVENT SESSION [Demo Database NAV_deadlock_monitor]
          ON SERVER
            ADD Target package0.event_file
          (
            SET filename=N'C:\logging\mydeadlocks.xel',max_file_size=(10240)
          )
      

    For more information see Alter an Extended Events Session and Targets for Extended Events in SQL Server.

  2. Create a view in the Dynamics NAV database that uses the new event_file target.

    You can create this view based on the default dbo.deadlock_report_ring_buffer_view view. To use the event_file target, you change xt.target_name = N'ring_buffer' to xt.target_name = N'event_file'. For example:

    USE [Demo Database NAV]
    GO
    
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE VIEW [dbo].[deadlock_report_event_file_view] AS
        SELECT target_data AS event_raw_data
        FROM sys.dm_xe_session_targets AS xt INNER JOIN sys.dm_xe_sessions AS xs
        ON xs.address = xt.event_session_address
        WHERE xs.name = N'Demo Database NAV_deadlock_monitor' AND xt.target_name = N'event_file'
    GO
    
  3. Change the Dynamics NAV database synonym dbo.syn_deadlock_event_view to point to the deadlock report event file view that you created.

    This synonym is used by the Microsoft Dynamics NAV Server to query the deadlock data. To alter a synonym, you first drop it, and then create a new synonym that has the same name. For example:

    DROP SYNONYM [dbo].[syn_deadlock_event_view]
    GO
    
    CREATE SYNONYM [dbo].[syn_deadlock_event_view] FOR [dbo].[deadlock_report_event_file_view]
    GO
    

Note

You can delete the ring_buffer target if you like. However, this is not required.

Configure the Microsoft Dynamics NAV Server instance

To log deadlocks, you must enable deadlock logging on the Microsoft Dynamics NAV Server instance. You can enable deadlock logging by using the Microsoft Dynamics NAV Server Administration tool or the Set-NAVServerConfiguration cmdlet in the Microsoft Dynamics NAV Administration Shell.

  • To enable deadlock logging by using the Microsoft Dynamics NAV Server Administration tool, open the server instance for editing, and then select the Enable Deadlock Monitoring check box in the Database section.

    For more information about how to use the Microsoft Dynamics NAV Server Administration tool, see Microsoft Dynamics NAV Server Administration Tool.

  • To enable logging by using the Set-NAVServerConfiguration cmdlet, include the keyname EnableDeadlockMonitoring keyvalue true parameters in the command, such as:

    Set-NAVServerConfiguration -ServerInstance DynamicsNAV -KeyName EnableDeadlockMonitoring -KeyValue true
    

    For more information about how to use the Microsoft Dynamics NAV Administration Shell, see Microsoft Dynamics NAV Windows PowerShell Cmdlets and Set-NAVServerConfiguration Cmdlet.

Viewing Deadlocks in the Windows Event Log

Similar to other errors and events in Dynamics NAV, you can monitor deadlocks by using Event Viewer on the computer running Microsoft Dynamics NAV Server. Deadlocks are recorded as warnings in the Microsoft Dynamics NAV Server Admin channel log in the Applications and Services Logs. For general information about how to view the Microsoft Dynamics NAV Server logs, see Monitoring Dynamics NAV Server Events Using Event Viewer.

Deadlock event overview

Deadlock event log entries have the event ID 705 and task category 33 (TelemetryData). The following table describes some of important information that is included in deadlock log entry:

Information Description
serverInstanceName Specifies the Microsoft Dynamics NAV Server instance on which the event occurred.
user Specifies the Dynamics NAV user account that ran the transaction that caused the event.
AL ObjectType Specifies the Dynamics NAV object in C/AL that ran the transaction, such as a page or report.
AL ObjectNumber Specifies the ID of the object that was run.
AL ScopeName Specifies the C/AL function that ran the transaction that caused the event.
SQL Server deadlock XML report Includes the deadlock report that was recieved from SQL Server. For more information, see Analyze Deadlocks.

Note

The system cannot record information about C/AL code that was executed on a different Microsoft Dynamics NAV Server. Therefore, the three attributes AL ObjectType, AL ObjectNumber and AL ScopeName might be empty in a given event log entry.

View a graphical representation of the deadlock event

To view a graphical representation of the deadlock, perform the following steps:

  1. Open the deadlock event in Event Viewer.
  2. On the General tab, go to the SQL Server deadlock xml report section, and then copy the text in the deadlock tag (including the start and end tag) to a text editor such as Notepad or Visual Studio Code.
  3. Save the file as a .xdl type.
  4. Open the file in SQL Server Management Studio.

Filter on deadlock events

All deadlock events have the trace tag 00000DI. If you only want to see deadlocks events in the log, you can use this tag in an XML path filter on the log, as shown in the following example:

<QueryList>
  <Query Id="0" Path="Microsoft-DynamicsNAV-Server/Admin">
    <Select Path="Microsoft-DynamicsNAV-Server/Admin">
                 *[EventData[Data[@Name='tag'] and (Data='00000DI')]]
               </Select>
  </Query>
</QueryList>

For more information about XML filtering, see Advanced XML filtering in the Windows Event Viewer.

See Also

Monitoring Microsoft Dynamics NAV Server Events
Monitoring SQL Database Locks
Configuring Microsoft Dynamics NAV Server
Use SQL Server Management Studio