Server Class LifeCycle

From RAD Studio
Jump to: navigation, search

Go Up to DataSnap Server Application

Server Class LifeCycle

The TDSServerClass metaclass has a LifeCycle property that determines the situations where new instances of server methods class are created, and when existing instances are reused or destroyed.

LifeCycle Property Types

The options for this property are Server, Session, and Invocation. In general, they can be interpreted as:

  • Server: one instance is created per running server (singleton).
  • Session: one instance is created per active client connection.
  • Invocation: a new instance is created for each invocation from a client (stateless).

REST Clients

For a REST client connection, if Session LifeCycle is used on the server class, it behaves like Invocation LifeCycle.

Connection Loss

If a client abruptly loses connection (non-REST connections), then, by default, the OnDisconnect event is not fired. This is because the socket remains open until an I/O operation is attempted and fails. If the LifeCycle property is Session, then the server class instance will be dangling, and only be freed when the server is stopped. If the LifeCycle property is set to Invocation and the connection was lost during an invocation (before the response was sent to the client), then the server class will, again, not be destroyed until the server is stopped.

However, if your OS is configured to use keep-alive packets for all TCP/IP connections, based on its configuration, you can control this behavior using the EnableKeepAlive and DisableKeepAlive methods. For more details, go to Monitoring and Controlling DataSnap TCP/IP Connections - Detecting Graceless Disconnects section.

Note: For more information regarding Server Class LifeCycle, please visit Delphi Labs to see the second episode of Delphi Labs Videos by Paweł Głowacki.