Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: TDSServerClass.OnDestroyInstance & EventObject.ServerClassInstance


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Dec 5, 2014 2:43 PM Last Post By: Mathias Burbach
Mathias Burbach

Posts: 42
Registered: 12/8/99
TDSServerClass.OnDestroyInstance & EventObject.ServerClassInstance  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 27, 2014 4:28 AM
Hello Folks!

I try to check for a certain condition when my TDSServerClass called TsvmPerson is destroyed. But unfortunately the DSDestroyInstanceEventObject.ServerClassInstance is always nil in my TDSServerClass.OnDestroyInstance event handler (Delphi XE6).

Here is what I try to achieve:
procedure TsrvContainer.sclPersonCreateInstance(DSCreateInstanceEventObject: TDSCreateInstanceEventObject);
begin
  DSCreateInstanceEventObject.ServerClassInstance := TsvmPerson.Create(nil);
end;
 
procedure TsrvContainer.sclPersonDestroyInstance(DSDestroyInstanceEventObject: TDSDestroyInstanceEventObject);
var
  svmPerson: TsvmPerson;
begin
  if DSDestroyInstanceEventObject.ServerClassInstance is TsvmPerson then
  begin
    svmPerson := DSDestroyInstanceEventObject.ServerClassInstance as TsvmPerson;
    if svmPerson.HasChangedAppointment then
      DoGoogleCalendarSyncronisation;
    svmPerson.Free;
  end;
end;


So I am looking after the creation of my server class myself but need to check if some appointments have changed in order to let the ServerContainer create a thread to synchronize Google Calendar (e.g. TsrvContainer.DoGoogleCalendarSyncronisation).

But DSDestroyInstanceEventObject.ServerClassInstance is always nil. At the beginning inside TDSServerClass.DestroyInstance DestroyInstanceEventObject.ServerClassInstance still has a value but then the Delphi source code sets it to LDataModule, which is in turn LAdapter.GetDataModule(DestroyInstanceEventObject.ServerClassInstance). LDataModule is nil and the subsequent call of FDestroyInstanceEvent(DestroyInstanceEventObject) passes in a DestroyInstanceEventObject instance with a ServerClassInstance of nil.

The help file states on Datasnap.DSServer.TDSServerClass.OnDestroyInstance:
"Use this event to override the default destruction of server class instances. OnDestroyInstance allows for custom initialization and custom object pooling if the LifeCycle property is set to Datasnap.DSNames.TDSLifeCycle.Invocation constant."

I am misinterpreting something here? What should I do to get hold of my server class instance before it is destroyed entirely?

Thanks for a short answer in advance.

Salut,
Mathias
Jeff Chojnacki

Posts: 5
Registered: 5/4/02
Re: TDSServerClass.OnDestroyInstance & EventObject.ServerClassInstance  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 5, 2014 9:08 AM   in response to: Mathias Burbach in response to: Mathias Burbach
Mathias Burbach wrote:
Hello Folks!

I try to check for a certain condition when my TDSServerClass called TsvmPerson is destroyed. But unfortunately the DSDestroyInstanceEventObject.ServerClassInstance is always nil in my TDSServerClass.OnDestroyInstance event handler (Delphi XE6).

Here is what I try to achieve:
procedure TsrvContainer.sclPersonCreateInstance(DSCreateInstanceEventObject: TDSCreateInstanceEventObject);
begin
  DSCreateInstanceEventObject.ServerClassInstance := TsvmPerson.Create(nil);
end;
 
procedure TsrvContainer.sclPersonDestroyInstance(DSDestroyInstanceEventObject: TDSDestroyInstanceEventObject);
var
  svmPerson: TsvmPerson;
begin
  if DSDestroyInstanceEventObject.ServerClassInstance is TsvmPerson then
  begin
    svmPerson := DSDestroyInstanceEventObject.ServerClassInstance as TsvmPerson;
    if svmPerson.HasChangedAppointment then
      DoGoogleCalendarSyncronisation;
    svmPerson.Free;
  end;
end;


So I am looking after the creation of my server class myself but need to check if some appointments have changed in order to let the ServerContainer create a thread to synchronize Google Calendar (e.g. TsrvContainer.DoGoogleCalendarSyncronisation).

But DSDestroyInstanceEventObject.ServerClassInstance is always nil. At the beginning inside TDSServerClass.DestroyInstance DestroyInstanceEventObject.ServerClassInstance still has a value but then the Delphi source code sets it to LDataModule, which is in turn LAdapter.GetDataModule(DestroyInstanceEventObject.ServerClassInstance). LDataModule is nil and the subsequent call of FDestroyInstanceEvent(DestroyInstanceEventObject) passes in a DestroyInstanceEventObject instance with a ServerClassInstance of nil.

The help file states on Datasnap.DSServer.TDSServerClass.OnDestroyInstance:
"Use this event to override the default destruction of server class instances. OnDestroyInstance allows for custom initialization and custom object pooling if the LifeCycle property is set to Datasnap.DSNames.TDSLifeCycle.Invocation constant."

I am misinterpreting something here? What should I do to get hold of my server class instance before it is destroyed entirely?

Thanks for a short answer in advance.

Salut,
Mathias

If you have not seen the QC yet. http://qc.embarcadero.com/wc/qcmain.aspx?d=126367
We are working around this by storing our TDSServerModules in the Session dictionary. We free the modules on session close.

Jeff
Mathias Burbach

Posts: 42
Registered: 12/8/99
Re: TDSServerClass.OnDestroyInstance & EventObject.ServerClassInstance  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 5, 2014 2:43 PM   in response to: Jeff Chojnacki in response to: Jeff Chojnacki
Jeff Chojnacki wrote:
If you have not seen the QC yet. http://qc.embarcadero.com/wc/qcmain.aspx?d=126367
We are working around this by storing our TDSServerModules in the Session dictionary. We free the modules on session close.
Jeff

Hello Jeff,

Thanks for your reply.

No, I had not seen your QC entry. I will vote on it as soon as QC allows me to gain some vote by removing them elsewhere (right now I run into a "qryVoteOnBug: Field 'DB_KEY' not found" when trying to remove votes).

So you add your server module into the session during OnCreateInstance and free it during OnDestroyInstance. There should always be one server module in the session or is there a way how a client could call a second server method before the first call returned?

Thanks for the hint and the link to the QC entry.

Salut,
Mathias
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02