

- #Net no exception thrown for deadlock sql .exe
- #Net no exception thrown for deadlock sql update
- #Net no exception thrown for deadlock sql code
exe WAITING Idle NO HOLDER SQL * Net message from clientġ34 ACTIVE sqlplus. exe 60 qmqpmbmyhxn WAITED SHORT TIME Network NOT IN WAIT SQL * Net message to clientġ4 INACTIVE sqlplus. SQL > set time on timing on 12 : 24 : 41 SQL > select sid, status, program, sql_id, state, wait_class, blocking_session_status, event from v $ session where schemaname = 'LALIT' and program = 'sqlplus.exe'

Let’s look at the session details from another session, lets call it SESSION 3 for the sake. So, v$session details when viewed in SESSION 2, i.e. exe 5 x0zg4qwus29v WAITING Application VALID enq : TX - row lock contentionĮlapsed : 00 : 00 : 00.00 12 : 22 : 18 SQL > exe 60 qmqpmbmyhxn WAITED SHORT TIME Network NOT IN WAIT SQL * Net message to clientġ34 ACTIVE sqlplus. SID STATUS PROGRAM SQL_ID STATE WAIT_CLASS BLOCKING_SE EVENT Let’s look at the session details from SESSION 2 – 12 : 22 : 15 SQL > select sid, status, program, sql_id, state, wait_class, blocking_session_status, event from v $ session where schemaname = 'LALIT' and program = 'sqlplus.exe'

#Net no exception thrown for deadlock sql update
SESSION : 1 12 : 16 : 15 SQL > UPDATE t_test SET col_1 = 8 WHERE col_2 = 4 Īt this point, SESSION 2 is the victim of deadlock, SESSION 1 is still waiting. SESSION : 2 12 : 16 : 04 SQL > UPDATE t_test SET col_1 = 6 WHERE col_2 = 4 1 row updated. SESSION : 1 12 : 16 : 06 SQL > UPDATE t_test SET col_1 = 5 WHERE col_2 = 2 1 row updated. Note the time of each transaction, I have set time on timing on for a better understanding. SQL > INSERT INTO t_test VALUES ( 3, 4 ) 1 row inserted SQL > INSERT INTO t_test VALUES ( 1, 2 ) 1 row inserted Let’s take a look at a simple example of deadlock situation using a single table with two rows : SQL > CREATE TABLE t_test ( col_1 NUMBER, col_2 NUMBER ) Table created Proper exception handling and error logging could lead us to the database objects involved in creating the deadlock. But just saying that doesn’t fix anything, and the herculean task is to replicate the deadlock situation. We know that a deadlock detected message is actually a clue to understand that it is a application design level issue. However, I would still be interested in knowing if there is a general way to recover from deadlocks on a read.Being a database application developer, a lot of times we come across deadlock errors. That way the locks on the rows can be removed right away. I could store the ids returned in a List and loop through that.
#Net no exception thrown for deadlock sql code
This code has been working fine for a while, but we are running quite a bit of code for each record, so I can imagine one of those processes is creating a lock on the initial table being read through.Īfter some thought, Scottie's suggestion to use a data structure to store the results would likely fix this situation. (No, there is no way to perform the update in the initial query, many other things occur for each record that is returned). Then I iterate through that list of ids using the Read function and run an update process on that record, which will eventually update a value for that record in the database. This case is not easy to reproduce so I'd like to be sure about it before I modify any of my code.Īs requested, some more info about my situation: In one case, I have a process that performs a query that loads a list of record ids that need to be updated. Will retrying Read after a deadlock skip any rows? Also, should I call Thread.Sleep between retries to give the database time to get out of the deadlock state, or is this enough. Is this the right approach? I want to be sure that records aren't skipped in the reader. If (ex.ErrorCode != 1205 || -DeadLockRetry = 0) Like this: public class MyDataReader : SqlDataReader I could create my own class that inherits from SqlDataReader which simply overrides the Read function with retry code. I was thinking about using a similar retry process.

This worked very well for the case where the deadlock happened during the initial query execute, but now we are getting deadlocks while iterating through the results using the Read() function on the returned SqlDataReader.Īgain, I'm not concerned about optimizing the query, but merely trying to recover in the rare case that a deadlock occurs. If (err.Number != 1205 || -DeadLockRetry = 0) throw an exception if the error is not a deadlock or the deadlock still exists after 5 tries To fix this we've added retry code to simply run the query again, like this: //try to run the query five times if a deadlock happends In the past, we've had some deadlocks that occur while calling the ExecuteReader function on our SqlCommand instance. This is usually very rare as we have already optimized our queries to avoid deadlocks, but they sometimes still occur. NET application show that it occasionally deadlocks while reading from Sql Server.
