Question

I have problem on understanding how to work with the foreachsession method of the TDSSessionManager.Instance. (I need an example if anyone have). a real example.

I want to do the following: one client connect to my DataSnap server. I save one value in the session. like this ... session.PutData('IDRETEA', "1");

I want to deny other clients connecting with the same value until the first connection finish his job.

how to iterate thru all the session and compare IDRETEA with the value finded in my current session? (based on some bussines logical).

I kinda want to implement this behavior on procedure TsrvContainer.dssServerMainConnect(DSConnectEventObject: TDSConnectEventObject);

here I have all values and I can raise an exception if I find another user and the client will know to resume the job (1,2,3 minute later), the server being "busy" now.

Was it helpful?

Solution

You can provide an anonymous method to the ForEachSession, like this:

procedure TServerContainer1.Test;
var
  MyCurrentSession: TDSSession;
begin
  MyCurrentSession := TDSSessionManager.Instance.GetThreadSession;

  TDSSessionManager.Instance.ForEachSession(
    procedure(const Session: TDSSession)
    begin
      // This procedure will be called for each session in the TDSSessionManager
      if (Session.GetData('IDRETEA') <> '') and (MyCurrentSession.SessionName <> Session.SessionName) then
        raise Exception.Create('Server busy. Try again later.');
    end);

  MyCurrentSession.PutData('IDRETEA', 'busy');
end;

Actually you just need to provide a method with this signature:

procedure(const Session: TDSSession)

OTHER TIPS

type
  TForm1 = class(TForm)
...
  private
    FIDETREA_ToSearch:string;    // input flag
    FIDETREA_SessionName:string; // result

    procedure EachSessionSearchIDRETEA(const Session: TDSSession);
...
  end;

....

{ return on FIDETREA_SessionName last session found with data 'IDRETEA'=FIDETREA_ToSearch }

procedure TForm1.EachSessionSearchIDRETEA(const Session: TDSSession);
begin
  if Session.GetData('IDRETEA')=FIDETREA_ToSearch then
    FIDETREA_SessionName:=Session.SessionName;
end;


procedure TForm1.Button2Click(Sender: TObject);  
begin
      FIDETREA_ToSearch:='1';   // input parameter
  FIDETREA_SessionName:=''; // result-> none
  Datasnap.DSSession.TDSSessionManager.instance.ForEachSession(EachSessionSearchIDRETEA);

  if FIDETREA_SessionName<>'' then // Found !!!!
    showmessage('Found: '+FIDETREA_SessionName)
  else
    showmessage('Not Found');
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top