XDataserver with more that one database

I would like to run an XDataserver that has the possibility to connect to different underlying databases.
I have studied the part of the manual concerning the IDBConnectionPool and IDBConnectionFactory and I get the feeling that it is possible, but I can't really get a grip on it.
Can you tell me what to change (e.g. using the samplecode from the manual) to implement a connection to different databases? Preferably with their own ConnectionPool maximum.

Hi Ronald,


you should simply create one different Factory/Pool for each database you want to connect to. Then create different TXDataServerModule (on different URL's) for each database. That's the approach users are using for multi-tenant applications.


Seems logical.
Do you think it's possible to do this in a flexible way?
E.g. a TObjectList of a class with a TXDataServerModule and a databasename?
I'm not sure how much databases are needed and it would be nice to load these parameters from an inputfile and to create the connections in a flexible way.

Yes, it's possible. You just need to leave the creation of your pool (or factory) in a separate function where you receive the database name as parameter:


function TFoo.CreateFactory(DBName: string): IDBConnectionFactory;
begin
end;

This way the DBName parameter will be captured by the anonymous method you use to create the connection for each different DBName you call CreateFactory method.

I think I can squeeze that easily in my existing functions. Thank you, Wagner!

I think the way you proposed above is a bit of overkill for our server.
I would like to work with 2 or more datasets, represented as a database with the same server. If I could just provide an ID that could solve it.
I have the code below. As you can see I would like to provide the anonymous method with a databaseid so it can open the right one. Is something like that possible?


    FServer := THttpSysServer.Create;
    FDBConnectionFactory := TDBConnectionFactory.Create(
       // Anonymous method to open IDBConnnection Interface
       function: IDBConnection  // I would like to provide an ID here
       var
          DM: TDM;
        begin
          // Create Datamodule
          DM := TDM.Create(nil);
          //  Connect to the database
          DM.ConnectPostgreSQL(ID); // ID opens the correct database
          Result := TFireDacConnectionAdapter.Create(DM.FDConnection1, DM);
        end
    );
    FConnectionPool := TDBConnectionPool.Create(nMax, FDBConnectionFactory);
    FXDataServerModule := TXDataServerModule.Create(XDataServerBaseURL, FConnectionPool);
    FServer.AddModule(FXDataServerModule);
    FServer.Start;


That wouldn't work because XData works with a connection pool. The factory might create the connection upon one request, but then that connection is made available for further requests. You can simply implement your own code to create a connection based on some HTTP header yourself, though. But you lose the high level stuff that XData provides to you.