Jump to content

Recommended Posts

Hello all,

 

I am returning to MARS after some years away.  I noticed that the new template application has a ServerConnectionPool data module.  Has anyone implemented connection pooling with a single, central datamodule and FDConnection, with a separate TDataModule for handling requests? If so, could someone give me a heads up on the best ways to set up the initial TFDConnection component on the connection-pool datamodule and how to connect this to my queries in the request handling datamodule?

Thanks

Stuart

Share this post


Link to post

You (as far as i know), need always to create (request for) a new FDConnection with each request, each request runs as it's own thread.

At what "ServerConnectionPool data module" you are referring to ?

(can not see somehting like this in the template folder?

 

I have examples of how to use the FDConnection in a normal request/resource if you need that.

 

uses  MARS.Core.RequestAndResponse.Interfaces,
      MARS.Data.FireDAC,
	  .....
	 
TMyResource = class
strict private
protected
  // connection can be left away, when referring to MAIN_DB in the .ini
  [Context, Connection('MyDB', False)] FD: TMARSFireDAC;
  [Context] FRequest : IMARSRequest;
public
  [Produces(TMediaType.APPLICATION_JSON)]
  function GetInfo([QueryParam('par1')] const APar1: string; const [QueryParam('par2')] APar2 : integer) : TMyInfo; 
end;

 

function TMyResource.GetInfo(const APar1: string; const APar2: integer): TMyInfo;
var
  fqry : TFDQuery;
begin
  if not FD.Connection.NewQuery(fqry) then raise Exception.Create('Error bij aanmaken van query');
  try
    fqry.sql.text := 'select * from ...........';
    fqry.Open();
    while not fqry.Eof do
    begin
       fqry.Next;
       .....
    end;
  finally
    fqry.Free;
  end;
end;
	
Edited by mvanrijnen
  • Like 1

Share this post


Link to post

I have attached an image which may explain better. 

 

This is from the latest MARS Template;  there is a ServerConnectionPool (TDataModule) that has a TFDConnection and is auto-created.  There is a ServerMainDB TDataModule that contains an FDQuery; this datamodule is created once per request by each endpoint that needs it. 

 

 

I have just noticed that the FDQuery is attached to the TFDConnection at designtime, so maybe that is sufficient? Maybe I have overlooked the obvious. 

ServerDataModule.png

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×