title: Managing SQLConnection / Datasnap through client-server disconnects
body: <p>In my Datasnap client application I use 1 TSQLConnection for my methods and ProviderConnection.</p>
  <p>The problems arise when the connection is lost. Both TSQLConnection.Connected and TSQLConnection.ConnectionState don't catch this.</p>
  <p>When my TSQLconnection is open, but I lose internet connection, or the server stops the Datasnap client application gives lots of errors. (Server Methods or ClientDatasets)</p>
  <p>I have created a function to manage my SQL connection for my server methods. but more problems arise when for example is close a ClientDataset that is connected through a TDSProviderConnection.</p>
  <p><strong>Q:</strong> How do you manage your client application to safely catch any disconnects on the TSQLconnection.<br />
  <strong>Q</strong>: How do you manage downtime, and what do you do with unsaved client state.</p>
  <p>Reconnection after downtime is not the problem.</p>
  <p>When calling a servermethod: This would throw exception.</p>
  <pre><code>tmpM:=TServerMethodsClient.Create(MYTSQLCONNECTION.dbxconnection,true);
  </code></pre>
  <p>So I wrote this next Method to obtain the TSQLCONNECTION from my datamodule with dummy method.</p>
  <pre><code>function TDMForm.DSConnection: TSQLConnection;
  var
    tmpM:TServerMethodsClient;
  begin
    result:=nil;
    if assigned(MYTSQLCONNECTION) then begin
      tmpM:=TServerMethodsClient.Create(MYTSQLCONNECTION.dbxconnection,true);
      try
        try
          tmpM.Ping;
          result:=MYTSQLCONNECTION
        except
          ReconnectForm.ShowModal; // has a reconnect button that tries to reconnect + shutdownbutton
          if ReconnectForm.modalresult=mrOK then
            result:=MYTSQLCONNECTION
          else
            MainForm.Close;
        end;
      finally
        tmpm.Free;
      end;
    end;
  end;
  </code></pre>
  <p>I wrote the Method this way because only way to find out if connection is lost is through dummy method which would throw same error... then I can query for reconnect or close program.</p>
  <p>Edit: I'm expecting some kind of general answer and guidelines, do's and don't. Not a correction of my code, this is just to show what I am doing at the moment.</p>