anchor.diff:      function Execute : integer;
     end;
   
  +  { TmDatabaseConnectionsCache }
  +
  +  TmDatabaseConnectionsCache = class
  +  strict private
  +    FIndex : TmStringDictionary;
  +    FList : TList;
  +    FCriticalSection : TCriticalSection;
  +  public
  +    constructor Create;
  +    destructor Destroy; override;
  +
  +    procedure CloseAll;
  +
  +    function GetConnection(const aConnectionInfo : TmDatabaseConnectionInfo): TmDatabaseConnection;
  +    procedure ReleaseConnection (aConnection : TmDatabaseConnection);
  +  end;
  +
   function GetLastSQLScript (var aTitle: string) : string;
   
   implementation
   
   uses
  -  syncobjs,
     mDatabaseConnectionImplRegister, mExceptionLog;
   
   var
     _LastSQLScript: string;
     _LastSQLScriptCriticalSection: TCriticalSection;
     _CreateImplementationCriticalSection: TCriticalSection;
  +  _ConnectionsCache : TmDatabaseConnectionsCache;
   
   procedure TraceSQL (const aSQLScript: string);
   begin
  
positive.code:   private
      FConnectionInfo : TmDatabaseConnectionInfo;
      FOwnsConnectionInfo : boolean;
  
      FImplementation : TmDatabaseConnectionImpl;
      procedure CreateImplementation;
  
negative.code:   Result := _LastSQLScript;
  end;
  
  { TmDatabaseConnectionsCache }
  
  constructor TmDatabaseConnectionsCache.Create;
  begin
    FIndex := TmStringDictionary.Create(true);
    FList := TList.Create;
    FCriticalSection := TCriticalSection.Create;
  end;
  
  destructor TmDatabaseConnectionsCache.Destroy;
  begin
    Self.CloseAll;
    FIndex.Free;
    FList.Free;
    FCriticalSection.Free;
    inherited Destroy;
  end;
  
  procedure TmDatabaseConnectionsCache.CloseAll;
  var
    i : integer;
    curConn : TmDatabaseConnection;
  begin
    FCriticalSection.Acquire;
    try
      for i := 0 to FList.Count -  1 do
      begin
        curConn := TmDatabaseConnection(FList.Items[i]);
        if curConn.Connected then
          curConn.Close;
      end;
    finally
      FCriticalSection.Leave;
    end;
  end;
  
  function TmDatabaseConnectionsCache.GetConnection(const aConnectionInfo: TmDatabaseConnectionInfo): TmDatabaseConnection;
  var
    curList : TObjectList;
    curConn : TmDatabaseConnection;
    i : integer;
  begin
    FCriticalSection.Acquire;
    try
      curList := FIndex.Find(aConnectionInfo.AsString) as TObjectList;
      if not Assigned(curList) then
      begin
        curList := TObjectList.Create(true);
        FIndex.Add(aConnectionInfo.AsString, curList);
      end;
      curConn := nil;
      for i := 0 to curList.Count - 1 do
      begin
        if not (curList.Items[i] as TmDatabaseConnection).FLocked then
        begin
          curConn := curList.Items[i] as TmDatabaseConnection;
          // it must be replaced as the old instance could be freed
          curConn.ConnectionInfo := aConnectionInfo;
          break;
        end;
      end;
      if not Assigned(curConn) then
      begin
        curConn := TmDatabaseConnection.Create(aConnectionInfo);
        curConn.FCached:= true;
        curList.Add(curConn);
      end;
  
      curConn.FLocked:= true;
  
      Result := curConn;
    finally
      FCriticalSection.Leave;
    end;
  end;
  
  procedure TmDatabaseConnectionsCache.ReleaseConnection(aConnection: TmDatabaseConnection);
  begin
    if Assigned(aConnection) then
      aConnection.FLocked:= false;
  end;
  
  
  { TmDatabaseConnectionsTandem }
  
  