Lost connection to MySQL Server during query

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
bryan.ray
Posts: 11
Joined: Sun 26 Jan 2014 18:07

Lost connection to MySQL Server during query

Post by bryan.ray » Fri 04 Jul 2014 15:43

Hello,
I am getting an error message/exception when trying to execute a query to a remote mySQL DB.
I am using Delphi XE6, FM Mobile app (iOS),Unidac 5.3.9

The raised exception is: Lost connection to MySQL Server during query
I have searched your forum for this error and found a few suggestions, but I still cannot get it to work. The exception is raised about 1-3 seconds after I execute the query. The variable setting on my server show I will not get a timeout until after 10 seconds.

Here's the code. I hope it is something simple to fix. This problem does not occur every time, but about 75% of the time. So I know the code works occasionally.

One of the suggestions on the forum was to put RetryMode := rmReconnectExecute in the OnConnectionLost event.

Regards,
Bryan

Code: Select all

Procedure TfrmPlotHModel.ConnectToDB;
Begin
  if not UniConnection1.Connected then
  Try
    UniConnection1.Connect;
  Except
    begin
      ShowMessage('Cannot Connect to the Database');
      Exit;
    end;
  End;
End;


procedure TfrmPlotHModel.tableViewHModelByModelRunItemSelect(Sender: TObject;
  ASection, ARow: Integer);
var
   selectedItem : TTMSFMXNativeUITableViewItem;
   adate : TDateTime;
   Points : TList<TTMSFMXNativeMKMapLocation>;
   Locations : TArray<TTMSFMXNativeMKMapLocation>;
   Point : TTMSFMXNativeMKMapLocation;
   Poly : TTMSFMXNativeMKOverlay;
   x, x1 : integer;
   s1, PrevModel : String;
begin
  ConnectToDB;
  ProgressModelPlot.Progress := 0;
  PrevModel := '';
  SelectedItem := tableViewHmodelByModelRun.GetItem(aSection,aRow, false);
  aDate := StrToDateTime(SelectedItem.Text);
  aQuery.SQL.Clear;  //TUniQuery
  aQuery.Close;
  aQuery.SQL.Add('SELECT * FROM stormdata WHERE storm_id = '+quotedStr(SelectedStormID)+
                 ' AND date = '+quotedStr(SQLDateTime(aDate)));
  aQuery.SQL.Add('ORDER BY tech, date_fore');

  s1 := aQuery.SQL.Text;

  aQuery.Prepare;
  aQuery.Open;
  x1 := 0;
  if not aQuery.IsEmpty then
  begin
    Points := TList<TTMSFMXNativeMKMapLocation>.Create;
    Try
      frmMain.Map.BeginUpdate;
      while not aQuery.Eof do
      begin
        ProgressModelPlot.Progress :=  (x1+1 / aQuery.RecordCount);
        Application.ProcessMessages;
        if (PrevModel <> '') and (aQuery.Fields[3].AsString <> PrevModel)
            and (Points.Count > 1) then
        begin
          //this is a new model, so plot the last one.
          PlotSingleModel(Points,GetRandomColor,3);
          Points.Clear;
        end;

        Point.Latitude := ConvertCoordinate(aQuery.Fields[5].AsString);
        Point.Longitude := ConvertCoordinate(aQuery.Fields[6].AsString);
        if Point.Latitude <> 0 then
          Points.Add(Point);

        PrevModel := aQuery.Fields[3].AsString;
        aQuery.Next;
        Inc(x1);
      end;
      //Plot the last set
      if Points.Count > 1 then
        PlotSingleModel(Points,GetRandomColor,3);
      frmMain.Map.EndUpdate;
    Finally
     Points.Free;
    End;
  end;
  ProgressModelPlot.Visible := False;
end;


PavloP
Devart Team
Posts: 149
Joined: Fri 24 Jan 2014 12:33

Re: Lost connection to MySQL Server during query

Post by PavloP » Mon 07 Jul 2014 14:39

To restore connection to MySQL server on connection break, use the OnConnectionLost event handler of the TUniConnection component.
You can find an article about frequent connection breaks in the UniDAC help: http://www.devart.com/unidac/docs/index ... etwork.htm
In addition, learn the principle of working with OnConnectionLost event in the UniDAC demo.

Post Reply