简体   繁体   中英

MySql Connection lost Delphi

I am writing DB app (with MyDac Component and MySql server) and i need your help. Imagine a Situation: 1. Application starts - connection exists, everything is OK. 2. Then the MySql server crashes or just connection problem occurs 3. User is trying to Select some data from DB BOOM! Application is goin to infinite loop (is trying to establish the connection)

I don't know what to do. I thing that it would be nice to put the query in a thread. I already know that i have to create a separate connection for this Thread. Due to lack of experience on working with threads i can't do this. It would be great if you write an example or give me an advices. I hope for your help,thank you

To avoid infinite attempts of reconnecting to the server, you can use a counter variable. For example, you can use the following code:


var
  RetryCount: Integer;

procedure TMainForm.MyConnection1ConnectionLost(Sender: TObject;
  Component: TComponent; ConnLostCause: TConnLostCause;
  var RetryMode: TRetryMode);
begin
  if RetryCount < 2 then begin
    Inc(RetryCount);
    RetryMode := rmReconnectExecute;
  end
  else begin
    RetryCount := 0;
    RetryMode := rmRaise;
  end;
end;

In this case, MyDAC will try to reconnect to the server twice, and in case of failure will generate an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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