Sql-server – Deadlock from select statements

deadlocksql serversql-server-2008

I am getting deadlock from 2 select statements. What is the reason for that? And how can I fix this issue?

enter image description here

Deadlocked processes:   11278 
Victim process: 5185
Lock type:  Index/(Key)

Both queries are the same

object: unicas_ux.dbo.Course, lock mode: Exclusive (X), index: NCIDX_Course_TermId

(@P0 bigint)select courses0_.TermId as TermId17_130_1_, courses0_.Id
as Id1_45_1_, courses0_.Id as Id1_45_0_, courses0_.ConvertedGrade as
Converte2_45_0_……courses0_.VerifiedCredits as Verifie14_45_0_,
courses0_.VerifiedDate as Verifie15_45_0_, courses0_.VerifiedGrade as
Verifie16_45_0_ from dbo.Course courses0_ where courses0_.TermId=@P0
order by courses0_.CreatedDate asc

Best Answer

Deadlock happens when one query acquires a lock on a object (rows, data pages, extent, tables etc) and other resource tries to access it. Smallest unit in SQL Server is data pages and SQL holds a lock on page while working on it. So, yes it is possible that two select statement can create deadlock.

Solution:

  1. WITH(NOLOCK) - If your data is fine to be read dirty
  2. Use Read committed snapshot or Snapshot isolation
  3. Try to keep transaction small and in batches
  4. Use bound connection - it will allow two connection to use same transaction & lock

This answer is based on your question, as your screenshot doesn't contain any information.