首页 > 代码库 > Firedac 数据连接池的应用

Firedac 数据连接池的应用

procedure TForm2.Button1Click(Sender: TObject);begin  if not FDConnection1.Connected then    FDConnection1.Connected := True;//  FDQuery1.SQL.Text := ‘select getdate()‘;  FDQuery1.Open(‘select getdate()‘);  Caption := FDQuery1.Fields[0].AsString;  //FDQuery1.Close;  FDConnection1.Connected := False;end;procedure TForm2.FormCreate(Sender: TObject);var  Def: IFDStanConnectionDef;begin  Def := FDManager1.ConnectionDefs.FindConnectionDef(‘PoolTest‘);  if Def=nil then    Def := FDManager1.ConnectionDefs.AddConnectionDef;  Def.Name := ‘PoolTest‘;  Def.Params.DriverID := ‘mssql‘;  Def.Params.Values[‘Server‘] := ‘JJW-PC‘;  Def.Params.Database := ‘TJZFDB‘;  Def.Params.UserName := ‘sa‘;  Def.Params.Password := ‘Sa12345‘;  Def.Params.Pooled := True;  Def.MarkPersistent;  Def.Apply;  FDConnection1.ConnectionDefName := ‘PoolTest‘;end;

经测试,动态添加连接池在第一次里有延迟,第二次就很多了,事件探查器中发现,多次点击button,最多只有2个连接。

 

Firedac 数据连接池的应用