Deadlock due to keylock and page lock in SQL Server - sql-server

We are facing deadlock for 2 to 3 times a day. When we analysed it is due to keylock and pagelock happening together.
We found deadlock is between the database replication job and other ETL job
Keylock is owned as Exclusive lock (x) by the database replication job on the clustered index column, and when ETL process is trying to get shared lock (s) on the same index it is failing as victim.
The page lock is owned by the ETL process as shared lock (s) and the replication job is getting an Intent Exclusive lock (ix) on the same.
When we try re-running the failed ETL job after some time it is getting completed. ETL job has only select query and no updates, inserts or deletes. It is only truncate and load. Further it is not happening in all jobs which is hitting the same server, only few jobs are getting failed. The replication job is set to run all the time.
Will SELECT(NO LOCK) help in this case? Or increasing the deadlock priority of ETL job?
Could anyone help how to resolve this deadlock?

Related

Getting DeadLock in SQL Server even after restarting server

I am getting deadlock error when updating a table :
Transaction (Process ID 67) was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
All other works perfectly fine. The thing making me uncomfortable is I have restarted the server itself 4-5 times how is the dead lock still on table.
Any suggestions?
Transaction (Process ID 67) was deadlocked on lock | communication buffer resources
This is probably caused by a bug while running a parallel query. Upgrade SQL Server to a later version, or disable parallelism for this query or the whole server.

Deadlock issue in MSFlow - Update data from CDS to SQL

I have created a Flow to update the GUID(the unique identifier of CDS entity records) into an SQL Server table From CDS whenever a new record is created in CDS. The flow is working fine If I create records one by one. But If I import multiple records(around 3000 records) from SQL to CDS using Dataflows, then I am getting the below deadlock error in Flows.
"Transaction (Process ID 74) was deadlocked on lock resources with
another process and has been chosen as the deadlock victim. Rerun the
transaction."
The dataflow refreshes the data on a scheduled basis. So, I could not resubmit the failed runs every time.
How to get rid of this deadlock issue? Or Is there any other approaches to update the SQL table in an efficient way?
I tried options like the degree of parallelism(10 records), retry policy. But no use. If I reduce the parallelly running records to 1, then it is running slowly and taking more than 1h for updating 1000 records.
If your query is a deadlock victim, you can create extended event session to capture details about this event. Then, having the deadlock graph you will find the real cause of your issue.
The graph will show you exactly what's the resource lock causing it and the involved statements.
you can try to change isolation level of your transactions in your connection with
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
to learn more
https://learn.microsoft.com/en-us/sql/connect/jdbc/understanding-isolation-levels?view=sql-server-ver15

Transaction was deadlocked on lock | communication buffer resources with another process and has been chosen as the deadlock victim. - SQL Server

I have running a concurrent SQL job to update the data in the same table. Which is making deadlock on the server-side. I have tried the locking method which does not solve my problem.

At the exact same time if 2 transactions try to update the same row of the same table.How the transaction management system manages here?

This is the question I have been asked in an interview. I tried to give the answer based on locking. Any of the one transaction will acquire exclusive lock but then the quetion is if both the transaction has started at the exact same time then how SQL server will decide which trnasaction will get the exclusive lock first. And what if there are mutiple transcation that get started at the same time to update the same row of same table, how it will be handled by MS SQL server?
One of the transaction will be granted the Xclusive lock, the other transactions will wait for the lock to be released.

SQL Azure - Transaction (Process ID 160) was deadlocked on lock resources with another process and has been chosen as the deadlock victim

We are experiencing the mentioned dead lock exception while doing CRUD on two SQL Server tables from parallel threads by calling Stored Procedures, here is the detailed scenario:
We have a desktop application where we are spinning up a code block in 100 - 150 parallel threads, the code block does insertion in TableA using SQL Bulk Copy and makes calls to three Stored Procedures, The stored procedures do insertion, updation and deletion in TableB based on some selection from TableA.
Soon as the application starts execution of the threads, SQL Server starts throwing the mentioned dead lock exception for a certain number of threads while some of the threads do run fine.
Exception Message:
Transaction (Process ID 160) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Any help in this regard is appreciated in advance.
Thanks.
Is this SQL Server or SQL Azure/Azure SQL DB? If it's "box" SQL Server, you might consider ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON. This will enable read versioning. It's still possible to encounter deadlocks in this state, but it's as close to a silver bullet as you're likely to get.
Read versioning changes the concurrency model in some subtle ways, so be sure to read about it first and make sure it's compatible with your business logic: https://msdn.microsoft.com/en-us/library/tcbchxcb(v=vs.110).aspx
Otherwise, Srivats's other suggestion about minimizing transaction scope is not always simple to implement but is still solid. To that list, I would add: Ensure that you have well-indexed query access paths, and verify that none of the queries within your transactions require full table or index scans.
The message is clearly evident that Transaction (Process ID 160) was deadlocked on lock resources with another process.The lock could be on different levels. The locks are not getting released before another thread could lock that particular resource. Try to kill that Process Id and check the workflow if there are any lock conflicts.

Resources