Search code examples
mysqlhibernatejpatimeoutexception

MySQL lock wait timeout and deadlock errors


I'm developing a mobile application whose backend is developed in Java and database is MySQL.

We have some insert and update operations in database tables with a lot of rows (between 400.000 and 3.000.000). Every operation usually doesn't need to touch every register of the table, but maybe, they are called simultaneously to update a 20% of them.

Sometimes I get this errors:

Deadlock found when trying to get lock; try restarting transaction

and

Lock wait timeout exceeded; try restarting transaction

I have improved my queries making them smaller and faster but I still have a big problem when some operations can't be performed.

My solutions until now have been:

  • Increase server performance (AWS Instance from m2.large to c3.2xlarge)
  • SET GLOBAL tx_isolation = 'READ-COMMITTED';
  • Avoid to check foreign keys: SET FOREIGN_KEY_CHECKS = 0; (I know this is not safe but my priotity is not to lock de database)
  • Set this values for timeout variables (SHOW VARIABLES LIKE '%timeout%';):
    • connect_timeout: 10
    • delayed_insert_timeout: 300
    • innodb_lock_wait_timeout: 50
    • innodb_rollback_on_timeout: OFF
    • interactive_timeout: 28800
    • lock_wait_timeout: 31536000
    • net_read_timeout: 30
    • net_write_timeout: 60
    • slave_net_timeout: 3600
    • wait_timeout: 28800

But I'm not sure if these things have decreased performance.

Any idea of how to reduce those errors?

Note: these others SO answer don't help me:

MySQL Lock wait timeout exceeded

MySQL: "lock wait timeout exceeded"

How can I change the default Mysql connection timeout when connecting through python?


Solution

  • Try to update less rows per single transaction.

    Instead of updating 20% or rows in a single transaction update 1% of rows 20 times.

    This will improve significantly your performances and you will avoid the timeout.

    Note: ORM are not the good solution for big updates. It is better to use standard JDBC. Use ORM to retrieve, update, delete few records each time. It speed up the coding phase, not the execution time.