Connecting the Client to DataSnap Server

From RAD Studio
Jump to: navigation, search

Go Up to DataSnap Client Application


TSQLConnection

The TSQLConnection component can be dropped from Tool Palette into a form or you can use it explicitly in your code. The driver that needs to be used is DataSnap. Turn off the connection property Login Prompt to prevent an authentication message window from appearing when you try to connect to a DataSnap server.

The following table lists the driver properties and their meanings:

Property Description
BufferKBSize Provides the maximum size of the packet that can be exchanged between the client and the server. The current value, 32, is recommended for the TCP/IP based protocol for optimal communication time.
CommunicationProtocol Accepts TCP/IP or HTTP, based on the transport type the server has.
ConnectTimeout Provides the expected connection time, in milliseconds. The connection time is defined as the interval between when the communication is initiated and the server socket is initially opened.
DSAuthPassword Currently used for HTTP authentication. The server is expected to have an authentication manager component linked to it that will process this value.
DSAuthUser Currently used for HTTP authentication. The server is expected to have an authentication manager component linked to it that will process this value in conjunction with the password provided. We recommend that both fields be filled or not.
HostName Machine name (or IP address) where the server runs.
PassWord Database user password for the case the server is expected to open a database connection and funnel client's database requests through it. If there is no server connection required, do not fill this property.
Port Provides the port value where the server listens for DataSnap connections. The value should match the one defined at the transport component on the server side.
ServerConnection Holds the name of the server connection that will provide the credentials for a database connection to be opened by the server. The server will act as a proxy for all database-specific client requests. Example: DSAdmin.GetConnection("ASACONNECTION").
URLPath Matches the context where the HTTP transport-based server listens for requests.
UserName Provides the user name to be used for the server connection. UserName is recommended to be empty if there is no server connection required.
DelegateName Optional. Stores the name of the delegate connection.
DelegateConnection Optional. Stores the name of the delegate driver. Delegate drivers provide an extra layer between the application and the actual driver. They are useful for connection pooling, driver profiling, tracing, auditing, and so on.


Notes:
  • It is important to have the connection in a context that allows the filters to be instantiated if such filters are defined at the server side. The unit that registers the filters must do that before a filtered connection is created.
  • If the HTTP protocol is to be used at run time, the DSHTTPLayer unit needs to be added to the uses list in the client application.

Closing a TSQLConnection

Once the data module generates a Datasnap connection, it creates a DBXConnection that establishes a tunnel between the DataSnap server and the client. Closing the TSQLConnection also closes the DBXConnection and breakes the tunnel. This tunnel will not be re-established if you open a connection again. Therefore, when closing the TSQLConnection, you should free the TServerMethodsClient object to force the creation of the tunnel again. See the following code snippet:

DataModule2.SQLConnection1.Close;
DataModule2.ServerMethods1Client := nil;
Warning: If you do not free the TServerMethodsClient object when closing the TSQLConnection and a new SQL connection is opened, the project will raise a TDBXError with the message 'Operation failed. Connection was Closed'.

Instantiate the proxy method class

The DBXConnection property of TSQLConnection can be used to instantiate the proxy method class. Explicit calls can be made for server methods based on TSQLServerMethod instances, like in the following example:

 procedure TForm1.Button8Click(Sender: TObject);
 var
   reader: TDataSet;

 begin
   SQLServerMethod:= TSQLServerMethod.Create(nil);
   SQLServerMethod.SQLConnection:= SQLConnection1;

   try
     SQLServerMethod.ServerMethodName:= 'TDSUtilityMethods.echoOutStr';
     SQLServerMethod.Params[0].AsString := '123';
     SQLServerMethod.ExecuteMethod;
     memOutput.Text := SQLServerMethod.Params[1].AsString;
   finally
     SQLServerMethod.Close;
     FreeAndNil(SQLServerMethod);
   end;
 end;

TDSRestConnection

The TDSRestConnection component can be dropped from Tool Palette into a form or you can use it explicitly in your code. Turn off the connection property Login Prompt to prevent an authentication message window from appearing when you try to connect to a DataSnap server.

The following table lists the connection properties that need to be changed when you create a DataSnap Client Application:

Property Description
Host Machine name (or IP address) where the server runs.
Password Database user password for the case the server is expected to open a database connection and funnel client's database requests through it. If there is no server connection required, do not fill this property.
Port Provides the port value where the server listens for DataSnap connections. The value should match the one defined at the transport component on the server side.
Protocol Accepts HTTP or HTTPS, based on the transport type the server has.
UserName Provides the user name to be used for the server connection. UserName is recommended to be empty if there is no server connection required.

Note: If the HTTP protocol is to be used at run time, the DSHTTPLayer unit needs to be added to the uses list in the client application.

REST

Specific server methods can be accessed through the REST protocol. That can be achieved from web-based client applications based on JavaScript code, or native, using the DSHTTP unit.

Here is a code snippet of a native method that sends a GET command through the REST server service.

 procedure TForm1.SendGetRequest(var response: string; out responseCode: Integer);
 var
   http: TDSHTTP;

 begin
   http := TDSHTTP.Create;

   try
     response := http.Get(edtUri.Text);
   except
     response := http.ResponseText;
   end;
   responseCode := http.ResponseCode;

   http.Free;
 end;

See Also