Jump to content
damos

Database reconnection

Recommended Posts

Hi am new Onomnithread library , and i examine the TwoFish example i understand  how  Working Schedule works ,

My question is how can i restart the   FWorker.Schedule if database is not connected (for example unstable network) after a few seconds until database be connected ???

 

In the main thread a create an instance of TComclass

 

procedure TMainform.Button1Click(Sender: TObject);

begin
  acom:= TComclass.Create(nil);

  acom.OpenConnection(format(common.PostgresConnection,['wwwwwwwww','wwwwwwwwww','wwww']),

 procedure (Sender: TObject; FatalException: Exception)
     begin
        if assigned(FatalException) then
        Begin
         if FatalException is EDAerror then
       Begin
       if  (FatalException as EDAerror).ErrorCode=0 then
    Errormessage.Caption:=FatalException.message
                       else
Errormessage.Caption:=FatalException.message+inttostr((FatalException as EDAerror).ErrorCode);
       End
       else
       Errormessage.Caption:=FatalException.message;
     // ShowMessage('Failed to connect to the database! ' + FatalException.Message)

           end
           else
           Begin
           Errormessage.Caption:='ComServer database connected' ;
           End;
      end);

end;

 

Declaration of Tcomlass

 

procedure TComclass.OpenConnection(const databaseName: string;
  onConnectionOpen: TNotify);
begin
  FWorker.Schedule(
    FWorker.CreateWorkItem(databaseName),
    FWorker.Config.OnExecute(ConnectToDatabase).OnRequestDone(
      procedure (const Sender: IOmniBackgroundWorker; const workItem: IOmniWorkItem)
      begin
        if assigned(onConnectionOpen) then
        Begin
        onConnectionOpen(Self, workItem.FatalException);
        if workItem.IsExceptional
         then
         Begin
         fdataModule.lisconnection.ConnectString := workItem.Data.AsString;
         fdataModule.lisconnection.Connected := true;
           end;
         end;
      end
    ));
end;

 procedure TComclass.ConnectToDatabase(const workItem: IOmniWorkItem);

var
  dataModule: TdmComdb;
begin
   dataModule := (workItem.TaskState.AsObject as TdmComdb);
   dmComdblock.Acquire; //probably only necessary when using InterBase driver

    dataModule.lisconnection.ConnectString := workItem.Data.AsString;
    dataModule.lisconnection.Connected := true;
   try


  finally
  dmComdblock.Release;
   end;

end;

 

Edited by damos

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×