Getting deadlocks in MySQL

Posted by at on Stack Overflow See other posts from Stack Overflow or by at
Published on 2010-05-26T23:25:57Z Indexed on 2010/05/26 23:31 UTC
Read the original article Hit count: 123

We're very frustratingly getting deadlocks in MySQL. It isn't because of exceeding a lock timeout as the deadlocks happen instantly when they do happen. Here's the SQL code that is executing on 2 separate threads (with 2 separate connections from the connection pool) that produces a deadlock:

UPDATE Sequences SET Counter = LAST_INSERT_ID(Counter + 1) WHERE Sequence IS NULL

Sequences table has 2 columns: Sequence and Counter

The LAST_INSERT_ID allows us to retrieve this updated counter value as per MySQL's recommendation. That works perfect for us, but we get these deadlocks! Why are we getting them and how can we avoid them??

Thanks so much for any help with this.

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql