# Deadlock when reading job data from SQL database

**URL:** <https://discuss.hangfire.io/t/deadlock-when-reading-job-data-from-sql-database/8917>\
**Category:** question\
**Tags:** sql-server\
**Created:** [January 25, 2022, 4:14pm UTC](https://discuss.hangfire.io/t/deadlock-when-reading-job-data-from-sql-database/8917 "2022-01-25T16:14:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrEco](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/mreco/32/2756_2.png) [@MrEco](https://discuss.hangfire.io/u/MrEco)\
**Post date:** [January 25, 2022, 4:14pm UTC](https://discuss.hangfire.io/t/deadlock-when-reading-job-data-from-sql-database/8917/1 "2022-01-25T16:14:11Z")

</div>

I running following query to aggregate some job data:

```auto
SELECT
    B.JobId,
    SUBSTRING(B.Name, 1, CHARINDEX(',', B.Name) - 1) AS Name,
    LastState,
    C.CreatedAt AS LastStateCreatedAt,
    REPLACE(D.Value, '"', '') AS RecurringJobName,
    B.CreatedAt
FROM
(
    SELECT
        Id AS JobId,
        COALESCE(
            JSON_VALUE(InvocationData, '$.t'),
            JSON_VALUE(InvocationData, '$.Type')
        ) AS Name,
        StateId AS LastStateId,
        StateName AS LastState,
        CreatedAt
    FROM
        Hangfire.Job
) AS B
LEFT JOIN Hangfire.State AS C ON Id = B.LastStateId
LEFT JOIN Hangfire.JobParameter AS D ON (B.JobId = D.JobId AND D.Name = 'RecurringJobId')

```

The query works but crashes randomly with following error:

```auto
Microsoft.Data.SqlClient.SqlException (0x80131904): Transaction (Process ID 64) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)
   at Microsoft.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)
   at Microsoft.Data.SqlClient.SqlDataReader.ReadAsyncExecute(Task task, Object state)
   at Microsoft.Data.SqlClient.SqlDataReader.InvokeAsyncCall[T](AAsyncCallContext`1 context)

```

Any idea what might cause this issue?

---

<div class="post-metadata">

**Author:** ![MrEco](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/mreco/32/2756_2.png) [@MrEco](https://discuss.hangfire.io/u/MrEco)\
**Post date:** [January 28, 2022, 12:19pm UTC](https://discuss.hangfire.io/t/deadlock-when-reading-job-data-from-sql-database/8917/2 "2022-01-28T12:19:00Z")

</div>

Using `WITH(NOLOCK)` like here [https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.SqlServer/SqlServerMonitoringApi.cs#L543](https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.SqlServer/SqlServerMonitoringApi.cs#L543) solved the issue.
