简体   繁体   English

Delphi FireDac 后续参数化 MySQL StoredProcedure 执行失败,出现“查询期间与 MySQL 服务器失去连接”异常

[英]Delphi FireDac subsequent parameterized MySQL StoredProcedure execution fails with "Lost connection to MySQL server during query" exception

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:为了重现这个问题,让我们有以下两个超级简单的 SP,第一个没有任何参数,第二个只有一个 output 参数:

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:如果我们调用第一个 SP 两次,一次又一次,那么它可以毫无问题地工作:

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:但是如果我们调用第二个 SP 两次,一次又一次,那么我们会得到一个异常:

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.在现实生活场景中,我们必须在项目中使用不同的参数值频繁调用同一个 SP,因此每次释放然后重新创建 SP object 并不是一个好的解决方案。

This worked fine in older Delphi versions using AnyDac, but with FireDac we have this problem.这在使用 AnyDac 的旧 Delphi 版本中运行良好,但使用 FireDac 我们遇到了这个问题。

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看起来有人也遇到了这个问题,并将解决方案发布到 Embarcadero 的质量中心: 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.所以这是一个 FireDac 错误,要修复它,您必须按照上面链接指示的方式编辑 FireDac 的源代码。 I can confirm that it solves the problem.我可以确认它解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM