Page 1 of 1

DB2 Select statements causing deadlocks?

PostPosted: Wed May 01, 2013 8:12 pm
by paulmac1968
In a single thread, I have two queries running consecutively using the same Websphere pool connection. They are both SELECTs and both use some of the same tables (not my design, it was inherited). Is it possible for one of these selects to cause the other to deadlock?

Re: DB2 Select statements causing deadlocks?

PostPosted: Wed May 01, 2013 8:47 pm
by dick scherrer
Hello,

Yes, it is possible if they are running concurrently and are locking rows.

Re: DB2 Select statements causing deadlocks?

PostPosted: Wed May 01, 2013 8:56 pm
by paulmac1968
What's the best way to mitigate the risk? Could we change the Isolation level? It's currently at Read Stability. Would we be better off setting it to Cursor Stability?

Re: DB2 Select statements causing deadlocks?

PostPosted: Wed May 01, 2013 9:05 pm
by dick scherrer
Hello,

The only way a deadlock (not to be confused with a timeout) can occur is when 2 processes try to lock the same rows in a different order. The way to prevent this is to make sure the processes access the tables in the same order.

Re: DB2 Select statements causing deadlocks?

PostPosted: Wed May 01, 2013 11:45 pm
by paulmac1968
I'm not sure what you mean. Do you have a simple example?

Re: DB2 Select statements causing deadlocks?

PostPosted: Thu May 02, 2013 12:03 am
by Akatsukami
paulmac1968 wrote:I'm not sure what you mean. Do you have a simple example?

Process P1 locks resource R1, uses it, and unlocks it, then R2; process P2 locks R2, then R1.

They attempt to do this:
P1       P2
===      ===
Lock R1
         Lock R2
Lock R2 (can't, because P2 has it locked)
         Lock R1 (can't, because P1 has it locked)

Deadlock, right?

Now consider changes to P2 so that it uses the resources in the same order as P1:
P1         P2
===        ===
Lock R1
           Lock R1 (Can't, because P1 has it locked)
Unlock R1
Lock R2
           Lock R1 (Now works, because P1 no longer has it locked)
Unlock R2
EOJ        Lock R2
           Unlock R2
           EOJ

No dead lock, right?

Re: DB2 Select statements causing deadlocks?

PostPosted: Thu May 02, 2013 1:03 am
by paulmac1968
Crystal clear. Thx. I've been running for a long time on the assumption that locks prevented actions that manipulated data. Inserts, updates, deletes. That reads were okay.

Re: DB2 Select statements causing deadlocks?

PostPosted: Thu May 02, 2013 1:35 am
by dick scherrer
Hello,

"Some" reads may be ok. . . It is the reads that lock rows that cause the problem.