Sql-server – Why the application is blocked infinitely and SQL Server never timeout and kill the deadlock

deadlocksql server

I have a multi-thread application that can opened more than one database connection to perform database operation. These connections are using the same credential.

Here is a problem, the application has a thread A opened an transaction, and start deleting items from a table, but the same thread A trigger another thread which open a new connection to read same table and to look for more thing to delete. As you can imagine, the deleting table is locked, and you can't do select on it until its transaction is completed. As a result, the application is blocking itself.

This application logic need to be fix. However, here are my questions:

  1. Is there a way to have SQLServer detect this application deadlock and kill one of the open selection, similar to a deadlock victim exception, or some kind of timeout setting?

  2. Is is possible to write a SQL Agent, that detect this situation and kill one of the session?

  3. Why would SQL Server throw a deadlock victim exception if it is caused by different application, and not for the same application?

Thanks

Best Answer

Deadlocks and blocking locks are two different concepts that you need to understand.

A deadlock is a situation where process/action 1 is waiting for process/action 2 to finish and at the same time process/action 2 is waiting for process/action 1 to finish. In other words. They would wait forever since they are waiting on each other.

In your scenario, something else is happening:

Process 1 is doing an action and has taken a lock on a resource to complete that action, Process 2 now wants to start a action that requires a lock on the same resource. Process 2 now has to wait for process 1 to complete and the lock is released. The key here is that at any given moment, none of the processes are waiting for each other (at the same time). One process is just waiting for the other process to finish an action on the same resource. They are not waiting for each other.

I hope that's clear.

On to how we fix your issue:

Can you post the Table definition, the indexes on the table and the delete select statement. We could have a look to see if there are ways to make the likelyhood of blocking locks less.