I was handed down a complex MySQL stored procedures that has TRANSACTION and tasked to find out an issue. I've followed several answers here in stackoverflow on "debugging a stored procedure" and produces my own process:
Log table:
CREATE TABLE `dt_sp_process_logs` (
`serial` varchar(20) NOT NULL,
`params` TEXT,
`message` TEXT NOT NULL,
`createdAt` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SP for logging:
DELIMITER %% CREATE PROCEDURE `spWriteLog`(
IN `serialCode` VARCHAR(20),
IN `params` TEXT,
IN `messageText` TEXT
)
BEGIN
INSERT INTO dt_sp_process_logs(`serial`, `params`, `message`)
VALUES(serialCode, params, messageText);
END %%
I've called the SP inside my to-be-debugged SP:
BEGIN
SET code = 0;
SET serialN = CONCAT(
'OR',
REPLACE(REPLACE(REPLACE(CURRENT_TIMESTAMP, ':', ''), '-', ''), ' ', ''),
(CAST(RAND() * 60 AS UNSIGNED) + 1)
);
START TRANSACTION
-- -------
-- long and
-- complex process
-- that determine
-- code value
-- -------
CALL spWriteLog(serialN, "", CONCAT("subTotalItems: ", subTotalItems, " || subTotal: ", subTotal));
-- -------
-- long and
-- complex process
-- that determine
-- code value
-- -------
IF code = 1 THEN
COMMIT;
ELSE
ROLLBACK;
END IF;
END
But it doesn't gets recorded in my dt_sp_process_logs
. My immediate conclusion is that because this SP is called inside transaction, it doesn't gets saved when the to-be-debugged procedure called for "rollback". Is there any other technique I can learn?