1

i have sometting strange in my application. I wonder what I did wrong or what I did not understand.

The goal is to create 2 functions in the ServerMethodsUnit to modify a variable of the web module.

For that,

I used the Datasnap Rest Server wizard with the following parameters

Stand Alone VCL Gui application No autorization, from Tdatamodule The server metjhods EchoString and ReverseString work well.

First I added in WebModuleUnit1 the variable var1:string;

TWebModule1 = class(TWebModule)
DSHTTPWebDispatcher1: TDSHTTPWebDispatcher;
DSServer1: TDSServer;
DSServerClass1: TDSServerClass;
ServerFunctionInvoker: TPageProducer;
ReverseString: TPageProducer;
WebFileDispatcher1: TWebFileDispatcher;
DSProxyGenerator1: TDSProxyGenerator;
DSServerMetaDataProvider1: TDSServerMetaDataProvider;

procedure DSServerClass1GetClass(DSServerClass: TDSServerClass;
var PersistentClass: TPersistentClass);
procedure ServerFunctionInvokerHTMLTag(Sender: TObject; Tag: TTag;
const TagString: string; TagParams: TStrings; var ReplaceText: string);
procedure WebModuleDefaultAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
procedure WebModuleBeforeDispatch(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
procedure WebFileDispatcher1BeforeDispatch(Sender: TObject;
const AFileName: string; Request: TWebRequest; Response: TWebResponse;
var Handled: Boolean);
procedure WebModuleCreate(Sender: TObject);
private
{ Déclarations privées }
FServerFunctionInvokerAction: TWebActionItem;
function AllowServerFunctionInvoker: Boolean;
public
{ Déclarations publiques }
var1:string;
end;

I added in ServerMethodsUnit1 2 functions in ServerMethodsUnit1

function SetVar1(astr:string):string; function GetVar1():string;

and a uses of Datasnap.DSHTTPWebBroker (interface) and WebModuleUnit1(implementation)

function TServerMethods1.GetVar1: string;
var oweb : TWebModule1;
begin
   oweb := TWebModule1(GetDataSnapWebModule);
   result := oweb.var1;
end;

function TServerMethods1.SetVar1(astr: string): string;
var oweb : TWebModule1;
begin
   oweb := TWebModule1(GetDataSnapWebModule);
   oweb.var1 := astr;
   result := oweb.var1;
end;

it all seemed ok !

I test my 2 function with http://localhost:xxxx/ServerFunctionInvoker

Everything works fine, when I have only one connection to my web server.

When I have multiple connections, it does not work anymore. I have the impression that the variable is shared by the different instances of the WebModule.

Example :

    Instance 1 (Chrome) -> SetVar1('TOTO')

    Instance 2 (Firefox) -> SetVar1('HELLO')

    Instance 1 (Chrome) -> GetVar1 : return 'TOTO'

    Instance 1 (Chrome) -> GetVar1 : return 'HELLO'

    Instance 1 (Chrome) -> GetVar1 : return ''

If I run the same function 3 times , I do not have the same answer!!!

It's as if the GetDataSnapWebModule function did not return the correct webmodule.

What did I do wrong?

How to share a variable (or a TfdmemTable) between 2 calls of a function rest

thank you in advance for your help.

@+ Romuald

  • There can be multiple instances of your TWebModule when more than one request/call is being served at a time. – Brian Apr 30 '18 at 15:33
  • yes, there are several instances, but when I repeatedly run the same function rest (Getvar1) in the same instance I do not have the same result. An alternative would be to know how to share data between several rest function. Where should I save this data ? thank you in advance for your help. @+ Romuald – Romuald Kler May 01 '18 at 19:39
  • You can write and read the shared data from a database or from a file in any of your server methods. – Freddie Bell May 02 '18 at 17:42

0 Answers0