In mysql how to identify what caused a table lock
The best way to do this is to enable the slow query log. It will log in a file queries that took more than X number of seconds to perform, as well as, the lock time it took.

You need to edit your /etc/my.cnf and add the following lines (you'll need to restart your mysql service for the changes to take effect):

slow_query_log = 1
slow_query_log_file = /var/log/slowmysql.log
long_query_time = 2
log-queries-not-using-indexes


A small explanation of each:


The first line enables the log.
The second tells the server where to store it.
The third one tells it how many seconds to wait in order to log the
query. I recommend you start with a high number (say 10 seconds),
and once your queries get optimized, reduce that number
The fourth line will enable you to log every query not properly
using an index. Although this might log quick running queries, it
will also help you spot which queries are not truly optimized using
the correct indexes.