For a reproduction of the issue, let's have the following two super simple SPs, the first without any parameters, the second with only an output parameter:
1. testsp_0_nopar:
CREATE DEFINER=`root`@`%` PROCEDURE `testsp_0_nopar`()
BEGIN
#do nothing
END
2. testsp_1_outpar:
CREATE DEFINER=`root`@`%` PROCEDURE `testsp_1_outpar`(OUT result INT)
BEGIN
SET result=100;
END
If we call the first SP two times, right after each other, then it works without any problem:
var
sp: TFDStoredProc;
begin
try
con.ResourceOptions.AutoReconnect := false; // <- this is important in our project
con.Connected := True;
sp := TFDStoredProc.Create(nil);
try
sp.Connection := con;
sp.StoredProcName := 'mydatabase.testsp_0_nopar';
sp.ExecProc;
sp.Close;
sp.ExecProc;
finally
sp.Free;
end;
except
on e: exception do
begin
ShowMessage(e.Message);
end;
end;
But if we call the second SP two times, right after each other, then we get an exception:
var
sp: TFDStoredProc;
begin
try
con.ResourceOptions.AutoReconnect := false; // this is important in our project
con.Connected := True;
sp := TFDStoredProc.Create(nil);
try
sp.Connection := con;
sp.StoredProcName := 'mydatabase.testsp_1_outpar';
sp.Params.Add('result', ftInteger, 1, ptOutput);
sp.ExecProc;
sp.Close;
sp.ExecProc; // <- this second sp call raises an exception
finally
sp.Free;
end;
except
on e: exception do
begin
ShowMessage(e.Message); // <- Lost connection to MySQL server during query
end;
end;
In the real life scenario we have to call the same SP frequently with different parameter values in our project, so it is not a good solution to free-then-recreate the SP object each time.
This worked fine in older Delphi versions using AnyDac, but with FireDac we have this problem.
Thank you very much for any help! :)
It looks like someone also ran into this problem, and posted the solution to Embarcadero's Quality Central: https://quality.embarcadero.com/browse/RSP-31692
So this is a FireDac bug, and to fix it, you have to edit FireDac's source the way the above link instructs you. I can confirm that it solves the problem.