Stuart Clennett 15 Posted July 14 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
mvanrijnen 127 Posted July 15 (edited) 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 July 15 by mvanrijnen 1 Share this post Link to post
Stuart Clennett 15 Posted July 15 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. Share this post Link to post
Andrea Magni 76 Posted July 22 Hi Stuart, have you generated your application using MARSCmd and selecting the MARSTemplate folder? I don't recognize that datamodule... BTW: MARS creates an instance of the resource per request. So if you have a TDatamodule descendant decorated with MARS attributes and registered as a REST resource, a new instance will be create per each incoming request. The TFDConnection, consequently, will be created too. The key point for using (FireDAC) connection pooling is pass through connection definition names (with Pooled parameter set to True) and not configuring the connection as one would do in a standalone application. Sincerely, Andrea Share this post Link to post
Stuart Clennett 15 Posted July 22 Hi Andrea, Sorry, it seems that I had this project from a while ago and I thought the server pooling was part of the template - my mistake. I am getting a little confused on how to set up a data module (created per request of course) that can pool connections, and respond to various end-points. I have the data-module already (inherits from TMARSFDDataModuleResource) and has most of my TFDQueries on there. I just need to have an instance created per endpoint request. Seems simple, but I can't figure it out. Is there a more comprehensive example somewhere? I see in the INI file created that we have FireDAC.MAIN_DB items commented out. Thanks Stuart Share this post Link to post
mvanrijnen 127 Posted July 23 (edited) 11 hours ago, Stuart Clennett said: Hi Andrea, Sorry, it seems that I had this project from a while ago and I thought the server pooling was part of the template - my mistake. I am getting a little confused on how to set up a data module (created per request of course) that can pool connections, and respond to various end-points. I have the data-module already (inherits from TMARSFDDataModuleResource) and has most of my TFDQueries on there. I just need to have an instance created per endpoint request. Seems simple, but I can't figure it out. Is there a more comprehensive example somewhere? I see in the INI file created that we have FireDAC.MAIN_DB items commented out. Thanks Stuart If you create the datamodule per request, why not set the connection in the datamodule as a property (set it from within the request). I do that in some projects. (needs some work maybe on the setter of connection property in the datamodule) So you would get something like: function TMyResource.GetInfo(const APar1: string; const APar2: integer): TMyInfo; var ldm : TMyDataModule; begin ldm := TMyDataModule.Create(); try ldm.MyConnection := FD.Connection; ldm.DoMyMethod(APar1, APar2); finally ldm.Free; end; end; Maybe you have a (simple) example of your code? Edited July 23 by mvanrijnen Share this post Link to post
Stuart Clennett 15 Posted July 23 Hi mvanrijnen Thanks very much - that's a great idea. The FD instance is declared as `[Context] FD : TMarsFireDAC` in the Resource I assume. I have a TBaseResource that all other resources inherit from (apart from Token) - so this will be easy to implement 🙂 For connection pooling do I need to use FD.ConnectionDefName as I am loading FireDAC.MAIN_DB into the FDManager at start up (as per the MARSTemplate ), or will FD.Connection also provide pooling? Thanks, (it's been a while since I've done _any_ DB programming at all) Share this post Link to post
Rolphy Reyes 0 Posted July 23 Hello! I'm interested in this topic. Can any of you provide some examples? Are your developments also multi-tenant and multi-database? Share this post Link to post
Stuart Clennett 15 Posted July 23 4 minutes ago, Rolphy Reyes said: Hello! I'm interested in this topic. Can any of you provide some examples? Are your developments also multi-tenant and multi-database? Neither in this case, I have done multi-tenancy before. Share this post Link to post