Search code examples
delphi-xe5datasnap

How to work with TDSSessionManager.Instance.foreachsession (Delphi XE5)


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.


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)