I have the following function which increments a value then retrieves it and returns it, one of the issues i am thinking i could face is between the update and select another transaction updates the currentVal value hence resulting in retrieving the wrong updated value, am i correct in thinking this?
CREATE FUNCTION INCREMENT()
RETURNS INT
BEGIN
DECLARE newVal;
UPDATE table_x SET currentVal=currentVal+1;
SELECT currentVal INTO newVal FROM table_x;
RETURN newVal;
END;
I have had a similar issue, and it is true that another update or insert transaction will update currentval. The way I resolved the issue was to put the update and select in a transaction which will cause the table to be locked. This will insure you always get currentval.