Skip to content

Instantly share code, notes, and snippets.

@joefrancia
Created June 13, 2020 22:51
Show Gist options
  • Save joefrancia/7de6c065c22e3e8b442c46458d6c1bfc to your computer and use it in GitHub Desktop.
Save joefrancia/7de6c065c22e3e8b442c46458d6c1bfc to your computer and use it in GitHub Desktop.
Avoid SQL contention in events
DELIMITER //
CREATE EVENT testlock_event ON SCHEDULE EVERY 2 SECOND DO
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
DO RELEASE_LOCK('testlock_event');
END;
IF GET_LOCK('testlock_event', 0) THEN
-- add some business logic here, for example:
-- insert into test.testlock_event values(NULL, NOW());
END IF;
DO RELEASE_LOCK('testlock_event');
END;
//
DELIMITER ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment