How to get pool connection in multidbconnection

I tried to modify the multitenantbyheader demo by entering two databases that have different structures. I want to make a service, and the service will be accessed by the two databases. I try to do it like this:

function TGetAllService.getAllData(AGuid: string): TALLDATA;

var
  Manager1: TObjectManager;
  Manager2: TObjectManager;
begin
  TXDataOperationContext.Current.Request.Headers.SetValue('database-name',DB1);
  Manager1 := TXDataOperationContext.Current.CreateManager(TMappingExplorer.Get('Model1'));

  TXDataOperationContext.Current.Request.Headers.SetValue('database-name',DB2);
  Manager2 := TXDataOperationContext.Current.CreateManager(TMappingExplorer.Get('Mode2'));




but the manager will connect first in the first place. How do I access both databases in the same service?

and I add a model to each entity.
I tried this approach by starting a new project without taking a multitenant demo.

unit ConnectionModule

class function TFireDacFirebirdConnection.CreateCustomeConnection(FileName: string): IDBConnection;
var
  DataModule: TFireDacFirebirdConnection;
begin
  DataModule := TFireDacFirebirdConnection.Create(nil);
  DataModule.Connection.DriverName := 'FB';
  DataModule.Connection.Params.Database := FileName;
  DataModule.Connection.Params.UserName := 'SYSDBA';
  DataModule.Connection.Params.Password := 'masterkey';
  Result := TFireDacConnectionAdapter.Create(DataModule.Connection, 'Firebird', DataModule);
end;



unit ServiceImplementation

var
  ConnectionA, ConnectionB: IDBConnection;
  ManagerA, ManagerB: TObjectManager;
begin
  ConnectionA := TFireDacFirebirdConnection.CreateCustomeConnection(DBa);
  ManagerA := TObjectManager.Create(ConnectionA, TMappingExplorer.Get ('Ma'));
  TXDataOperationContext.Current.Handler.ManagedObjects.Add(ManagerA);

  ConnectionB := TFireDacFirebirdConnection.CreateCustomeConnection(DBb);
  ManagerB := TObjectManager.Create(ConnectionB, TMappingExplorer.Get ('Mb'));
  TXDataOperationContext.Current.Handler.ManagedObjects.Add(ManagerB);



my question, is it safe to use this method?
I've tried it, and data from DBa and DBb can be returned correctly by XData.

Yes, it's safe, and makes more sense since your databases have different structures.

You could also use a connection pool instead of creating one IDBConnection for each request. 
Thanks Wagner,

but how to choose the connection that I want from the connection pool, for example in my pool I have 3 databases that I add when the application starts.

const
  HETT_DB = 'C:\database\HETT_.FDB';
  KWLL_DB = 'C:\database\KWLL_.FDB';
  GTAA_DB = 'C:\database\GTAA_.FDB';
  .....

procedure TForm1.FormShow(Sender: TObject);
begin
  ServerByHeaderModule.AddDatabase(HETT_DB);
  ServerByHeaderModule.AddDatabase(KWLL_DB);
  ServerByHeaderModule.AddDatabase(GTAA_DB);
  btStartStop.Click;
end;


procedure TServerByHeaderModule.AddDatabase(DBName: string);
var
  Pool: IDBConnectionPool;
begin
  Pool := CreateConnectionPool(DBName);
  FMultiPool.AddOrSetPool(DBName, Pool);
end;


The ServerByHeaderModle would automatically choose the pool based on the client request header. Since you are using a different approach, you could simply save a reference to the created pool in some global variable and retrieve a connection from it, if you know from which database you need to get a connection to.