I couldn't find a way around the issues described above, so instead used a different approach which I'll explain:
I created a table named transaction_properties
which has two columns: user_id
and connection_id
.
Before each transaction commit
, I INSERT
into the transaction_properties
table with the ID of the user (which I know at this stage), and the MySQL placeholder CONNECTION_ID()
, which outputs the current connection ID.
In my trigger, I use the following statements to find the user_id
for this transaction:
DECLARE user_id bigint;
SELECT `userid` INTO user_id FROM `transaction_properties`
WHERE `transaction_id` = CONNECTION_ID() LIMIT 1;
IF user_id IS NULL THEN
SET user_id = -1;
END IF;
I can then reference the declared variable user_id
in my INSERT
to the audit table elsewhere in the trigger.