DELIMITER $$

DROP PROCEDURE IF EXISTS `D_AUDIT` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `D_AUDIT`()
    SQL SECURITY DEFINER
BEGIN
	DECLARE done INT DEFAULT 0;
	DECLARE dummy_var INT DEFAULT 0;
	DECLARE cmd VARCHAR(100);
	DECLARE cur CURSOR FOR select concat('KILL ',id,';')  from information_schema.processlist WHERE (info like 'CREATE TABLE%ENGINE=CONNECT%' and time > 120);

	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

	DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
		DO RELEASE_LOCK('D_AUDIT_lock');
	END;

	SELECT IF(IS_FREE_LOCK('D_AUDIT_lock') = 0, 0, GET_LOCK('D_AUDIT_lock', 1)) INTO dummy_var;
  IF (dummy_var = 1) THEN
		OPEN cur;
		read_loop: LOOP
			SET done = 0;
			FETCH cur INTO cmd;
			IF done THEN
				LEAVE read_loop;
			END IF;

      SET @comm = cmd;
			PREPARE lr_query FROM @comm;
			EXECUTE lr_query;
			DEALLOCATE PREPARE lr_query;
		END LOOP;
		CLOSE cur;
	END IF;
	DO RELEASE_LOCK('D_AUDIT_lock');
	
END $$

DELIMITER ;