why deadlocks cannot be automatically handled by the lock monitor - sql-server

Why would sql-server lock monitor thread would not resolve deadlocks?
I don't have much details about this, but my friends complain about deadlocks occuring from time to time on SQL Server (2005 & 2008) databases.
As I know, Sql Server deadlock detection mechanism handles deadlock scenarios pretty well by choosing one of the processing as the victim and terminating/rolling it back, so the other process can complete, so no deadlocks would be left there to resolve.
Are there any conditions where SQL Server deadlock handling might fail, or it might be turned off?
thanks in advance

Deadlock handling can only result in one connection being killed.
This is the only resolution to a deadlock sitution, which happens when 2 process are blocking each other. So neither can proceed and one must be aborted

Related

Oracle deadlocks for two uncommon queries

I have dead lock: sql A is waiting on sql B.
----- Information for the OTHER waiting sessions -----
A is: delete from attachment where ID=:1
----- Current SQL Statement for this session (sql_id=audwbsf163w1n) -----
B is: delete from detail where ID=:1
deadlock trace log
My big question is. Why is possible that these two sql scripts are waiting on each other but they have nothing in common in database? It do not make any sense, that they are waiting on each other.
I'd advise care with the terminology used: "Deadlock" has a very specific meaning (below) and what you are implying in your problem description is a "blocking lock".
A deadlock is the classic “deadly embrace” problem. The “deadly embrace” happens when, say Task B, attempts to lock a row which is being held by another task, say Task A, and Task A is waiting for Task B to release a lock. Oracle will abort one of the transaction to prevent a perpetual wait condition.
Root Cause (general):
• Transactions deadlocked one another while waiting for resources. [from ora docs]
General Solution:
• Generally, this is a design flaw in the application.
• The deadlock is an application issue and must be fixed within the application;
there is no DB fix for a deadlock.
A deadlock will result in Oracle terminating one of the sessions and that session will receive an error indicating the reason. This happens with 2 to 3 seconds of it being detected.
A blocking lock on the other hand will remain indefinitely until the blocking session either commits, is rolled-back or is killed (which does a rollback).
Also, check for cascading operations generated as recursive SQL by the DBMS.

IsolationLevel, Lock Modes, deadlocks and sp_getapplock in MS SQL Server

I am new to SQL server and the deadlocking like issues. I read articles about it. I want to understand following things:
SQL server uses appropriate Lock Mode depending on the IsolationLevel I set while beginning the transaction. If this understanding is correct, what's the purpose of sp_getapplock ?
I am running into the SQL deadlock issue. Several instances of my ASP.NET application running on different servers access the same database. If I use the appropriate IsolationLevel on transactions, do I still need to acquire SQL lock using sp_getapplock?
(I know more appropriate solution is to identify problematic transactions and fix them. But still it's more of a theoretical question.)
Blocking and deadlocking are two different things. Please read this article for more details.
Following is copied from the article:
Developers and DBAs often think that their SQL Server instance is experiencing deadlocks when, really, it is experiencing severe blocking.
Blocking occurs when session A requests a lock on a resource (typically a row, page or table), but SQL Server cannot grant that lock because session B already holds a non-compatible lock on that resource.
This is a transient situation and can be completely resolved by the session B completing its work and releasing its locks. It is possible to have extensive blocking chains where multiple sessions are blocked waiting for a session that itself is blocked waiting for another session that is blocked and so on, repeating multiple time. However, at the head of the blocking chain will be a head ‘blocker’ that is not waiting for a lock. It may be waiting for some other resource, such as a latch, memory, or IO, but at least one session will not be waiting for a lock, and the blocking chain will clear as soon as the head blocker can continue processing.
A deadlock is different; it occurs when two or more sessions are waiting for each other, in such a way that none can complete. A deadlock can be viewed as a circular lock chain, where every single process in the blocking chain is waiting for one or more other processes in that same blocking chain.
sp_getapplock is not directly related to your problems. Applocks are custom defined locks, used by your application - for example when you need to synchronize with external data source, using some lengthy and complex procedure and you don't want multiple processes to run such procedure at once.
About deadlocks - you have to design your procedures to minimise deadlock possibility and then you need create some error handling to detect deadlocks and take appropriate action (eg retry 5 times, fail afterwards).
If you could post your deadlocking procedure code, then likely someone can help you redesign it.

What should happen when two transactions collide?

I have to implement an sql transaction as my assignment and show that i've chosen correct isolation level. The problem is im not sure what should happen when two transactions collide when isolation level is optimal. What i mean is that i dont know whether one of them should get cancelled due to the deadlock or complete successfully but have no effect (get rollbacked?). Im using MS SQL Server if it matters.
When transactions are in a deadlock situation, SQL Server chooses one as the victim of the deadlock.
That means that the victim transaction will be canceled and rollback whereas the other will keep on going as if nothing happened. SQL Server chooses the victim based on the difficulty (CPU cost) of rolling-back the victim's changes. The victim transaction will then error out with the error code 1205, which is quite explicit.
You can set the transactions' deadlock priorities yourself, as explained in the second link.
More details here :
http://technet.microsoft.com/en-us/library/ms178104(v=sql.105).aspx
And here :
http://technet.microsoft.com/en-us/library/ms186736.aspx
Btw, the fact that you use SQL Server matters a lot ! Some other DBMS would just keep the transactions in a lock forever, and others would just choose to error both transactions out. I never heard of one that would consider the transactions complete and roll them back, though...

What is livelock exactly? In SQL when/how does it happen? Anything T-SQL developer can do to avoid it?

I have encountered this jargon for SQL Server, I am just not able to understand what it is exactly it happens in SQL Server. I am looking at some SQL Server example where it occurs...
I have been searching on google but all keep getting is 2 people in corridor example.
A deadlock occurs when two processes compete for the same resources, but in an order which causes a stalemate. For example, A locks X, then tries to lock Y, while B has locked Y and tries to lock X. The key is that the two (or more) processes are preventing each other from doing anything.
A livelock occurs when there are overlapping shared locks that prevent another process from acquiring the exclusive lock it needs. The difference is that all of these overlapping processes continue to get their work done, so they are still "live" - only the victim is blocked until they are done. Which may be never on a busy enough, poorly designed system. :-) You may be able to overcome this situation by escalating the deadlock priority for your writers, but I'll be honest, this isn't a scenario I've seen very often, and I've worked with SQL Server since 6.5...

Is there a way to prevent from deadlocks (and what happens is one occurs and one is victimized)

I've tried to read an article on locks and deadlocks, and it just doesn't land, al the different kind of locks.
We're running 2 different processes which try to edit records in the same table.
The first process reads the new data and sends it to an external party and updates the status accordingly, the other one receives the receiveresults form the external party and updates the status accordingly.
Now we more and more get deadlocks (in which case one of the two is the victom and gets aborted). So far so good because you can anticipate on that, and try to rerun the statement, but then allways the same deadlock occurs.
So that's my first question: why does the same deadlock allways reoccur?
And second, is there a way to tell the dbms not to try and get an exclusive lock for a record (we update single records via a stored procedure) when another process is already reading and updating it, but 'wait at the side' till the other process is ready?
Or is that a much too simple thing to say about a deadlock?
and may be third, is there a way to ask LINQ to SQL which locks were causing the problem, so i get a little more insight of which parts of the process are causing the problem.
SQL Profiler is the best tool I've used to start trying to solve deadlock problems in SQL Server.
Start a trace:
On the Events Selection tab:
Uncheck all of the preselected events.
Check "Show all events".
Expand "Locks".
Check "Deadlock graph", "Lock:Deadlock", and "Lock Deadlock Chain".
Run the trace and capture the deadlock events.
View the deadlock events in SQL Profiler. The deadlock display is pretty good. You may still need to look at the raw XML to get additional details.
Using what you find from this first pass, may give you ideas on what to change, or suggest other events to trace in SQL Profiler.
As #Darryl Peterson indicates, SQL Profiler is a good tool to capture deadlock information. If you don't know when the deadlock will occur, you can set a SQL Server trace flag to capture the data.
DBCC TRACEON (1204)
When a deadlock occurs, information about the deadlock will be written to the SQL Server error log.
There are a number of ways to get a deadlock. Your first question "why does the same deadlock always occur" is probably a good sign. If the deadlock is repeatable, then you can catch it and fix it.
Regarding your second question, SQL Server is probably already telling one process to wait while the other completes. However, you can't avoid a deadlock by waiting. A deadlock is a situation where a process is trying to use a resource that is held by another process. But the other process is waiting for a resource that the first process is holding. Neither process will release a resource until it completes its work. Note that this is a simple explanation and there can be more complex deadlock conditions. The point is that processes in the deadlock will never be able to complete.
Once you learn more about the processes involved in the deadlock, you should be able to take steps to avoid it.

Resources