I have a method which is concurrently called. In that method I have a defined a transaction in which some DELETE operations and SELECT operations are happening on the parameter passed in the method. I try to invoke this method concurrently and pass different data at each instant. I am get an error:
Error 1205 : Transaction (Process ID) was deadlocked on resources with
another process and has been chosen as the deadlock victim. Rerun the
transaction
The body of method looks like this:
public void method(param){
//transaction starts
// delete operation on table 1
// select operation on table 1
// transaction is committed.
}
Let there be Transaction T1 and T2
I think this deadlock condition should not occur because in this case row level locking would have been applied on the basis of parameter I passed in the method. If Transaction T2 one wants to delete other data then Transaction T1 should not hinder it from deleting it. What is happening is that one transaction is being rolled back since I am catching SQLException therefore only one record is successfully getting deleted. Can anyone figure out why this doesn’t work?
First of all, depending on the indexes in use, some databases may use table level locks when you think they should be using row level locks. so, step 1 is to verify that you are in fact using row level locks.
second, are you using any automatic cascading which could be affecting other tables when you delete? and/or are there foreign key relationships involed. missing indexes in this situation can also cause problems.
third, sometimes updates can affect more than 1 row due to how the indexes are handled. some databases lock “chunks” of the index whenever there is an update (e.g. a row deletion). it’s possible that there are conflicts due to the locks on the indexes.