Event Errors
Errors inside events are silently logged to the MySQL error log. Use DECLARE HANDLER inside BEGIN...END to handle exceptions gracefully.
Errors inside events are silently logged to the MySQL error log. Use DECLARE HANDLER inside BEGIN...END to handle exceptions gracefully.
DELIMITER //
CREATE EVENT safe_cleanup
ON SCHEDULE EVERY 1 HOUR
DO
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
INSERT INTO event_errors (event_name, error_time) VALUES ("safe_cleanup", NOW());
END;
DELETE FROM sessions WHERE expires_at < NOW();
UPDATE stats SET last_cleanup = NOW() WHERE stat_key = "session_cleanup";
END//
DELIMITER ;
Without an error handler, a failed event is silently skipped — always log errors to an audit table.