The idea might be to catch a connection error, and then manage it with attempts. Sentence:
This function returns a description of the exception.
function GetStrException(ExceptObject: TObject; ExceptAddr: Pointer):String; var Buffer: array[0..1023] of Char; begin ExceptionErrorMessage(ExceptObject, ExceptAddr, Buffer, SizeOf(Buffer)); Result:=Buffer; end;
This is a simple idea to test!
procedure TForm1.Button2Click(Sender: TObject); var s,error:String; begin //a select as an example S := 'SELECT COUNT(*) FROM MyTable'; TRY WITH ADOQuery1 DO BEGIN SQL.Clear; SQL.Add(s);OPEN;END; Memo1.Lines.ADD(ADOQuery1.Fields[0].AsString); EXCEPT error:=(GetStrException(ExceptObject,ExceptAddr)); //using MySql my error case is when the connection is lost, so the error is "Server has gone away" if pos(error,'has gone away')>0 then begin Showmessage('Connection Error, please try again'); try sleep(1000) AdoConnection1.close; AdoConnection1.open; except Showmessage('The connection Error persists, please contact the support'); end; end; END; end;
A good solution is to centralize the insert / update and select procedures, then catch the error and try to adjust the situation once or twice before showing the message to the user
source share