I have a scenario in my app where simple INSERT statement under transaction is blocking DELETE statement on different rows in MySQL and eventually, DELETE statement session times out with lock time out error.
I have tried to explain the situation in a simple scenarios below. Note, adding index on the column helps as bit however, if I have 2 columns in WHERE clause of DELETE, I still see lock wait timeout. Also please note that after I start transaction, I need to do few other processing and hence transaction takes few mins to commit or roll back. It is hard to believe that even if I delete different records than what I am trying to insert, MySQL blocks it.
Session 1:-
mysql> SELECT @@GLOBAL.tx_isolation, @@tx_isolation, @@session.tx_isolation;
+-----------------------+----------------+------------------------+
| @@GLOBAL.tx_isolation | @@tx_isolation | @@session.tx_isolation |
+-----------------------+----------------+------------------------+
| READ-COMMITTED | READ-COMMITTED | READ-COMMITTED |
+-----------------------+----------------+------------------------+
1 row in set (0.02 sec)`
mysql> create table testtab(col1 int, col2 int);
Query OK, 0 rows affected (0.53 sec)
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO testtab values(1,1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO testtab values(2,2);
Query OK, 1 row affected (0.00 sec)
Session 2:-
mysql> SELECT @@GLOBAL.tx_isolation, @@tx_isolation, @@session.tx_isolation;
+-----------------------+----------------+------------------------+
| @@GLOBAL.tx_isolation | @@tx_isolation | @@session.tx_isolation |
+-----------------------+----------------+------------------------+
| READ-COMMITTED | READ-COMMITTED | READ-COMMITTED |
+-----------------------+----------------+------------------------+
1 row in set (0.00 sec)
mysql> DELETE FROM testtab where col1=3;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Any suggestions ?