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?
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.
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.