Wednesday, January 18, 2017

GET_LOCK problem

I saw an interesting problem with the use of MySQL's GET_LOCK today. A client sent a notification that they are getting a lot of the following type of errors:

DAEMON: job=importFILE; exception=System.Exception: GET_LOCK failed: 0 

I logged onto the database that was having the problems and at any given time, I see about 5 or 6 running connections with this:

SELECT GET_LOCK('MyTableLock',10)

The application was running code like this:

                @"START TRANSACTION;
                SELECT GET_LOCK('MyTableLock',10);
                INSERT INTO MyTable (id, customer_id, process_id, value) VALUES (-1,-1,null,'-');
                INSERT INTO MyTable (id, customer_id, process_id, value) SELECT -1, -1,(LAST_INSERT_ID() + @vnum - 1) + 20, '-' from MyTable limit 1;
                SELECT LAST_INSERT_ID() + 20;
                SELECT RELEASE_LOCK('MyTableLock');


                ROLLBACK;";

It appeared the code would issue a GET_LOCK which would be followed by an error before the RELEASE_LOCK happened. The error didn’t cause the session to terminate and it was released back into the pool with the lock still held.  

The code didn't reset sessions so the next connection from the connection pool would still have that lock held. At this point, there would no longer be anything running that would have needed that lock. 

For a work around, we restarted the application process and killed the sessions that were running the GET_LOCK. Ultimately the code needs to be re-written to take this into account.

Here is some reading on what GET_LOCK does:
http://techblog.procurios.nl/k/news/view/41405/14863/mysql-get_lock()-explained.html
https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html

No comments:

Post a Comment