FireDac Pooling

1.創建FDManager的ConnectionDef。並設置此Pooling爲True. api

2.創建Thread類進行多個FDConnection鏈接DB。 oracle

3.本列是oracle遠程數據.以下圖:ide

 

Open pooling是創建FDManger中的Connection. Thread按鈕是創建鏈接的測試。右邊的空白是顯示鏈接時間(爲Ticks)測試

時間單位換算:ui

1秒=1000毫秒
1毫秒=1000微秒
1微秒=1000納秒
1納秒=1000皮秒
1皮秒=1000飛秒
在C#中,1Ticks = 100納秒
 
如下爲代碼:
  
unit Unit1; interface

uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def, FireDAC.Phys, Vcl.Buttons, FireDAC.Comp.Client, FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.VCLUI.Wait, Data.DB, Vcl.StdCtrls, FireDAC.Moni.Base, FireDAC.Moni.RemoteClient,FireDAC.DApt, FireDAC.Phys.OracleDef, FireDAC.Phys.Oracle,System.Diagnostics; type TForm1 = class(TForm) FDManager1: TFDManager; SpeedButton1: TSpeedButton; FDConnection1: TFDConnection; BitBtn1: TBitBtn; Memo1: TMemo; FDMoniRemoteClientLink1: TFDMoniRemoteClientLink; FDPhysOracleDriverLink1: TFDPhysOracleDriverLink; procedure BitBtn1Click(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); private
    { Private declarations }
  public
    { Public declarations }
  end; TDBThread=class(TThread) private FForm:TForm1; FConn:TFDConnection; FQry :TFDQuery; procedure exePrc; public
    procedure Execute;override; constructor Create(AForm:TForm1); destructor Destroy;override; end; var Form1: TForm1; implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject); var i:integer; ATrhed:TDBThread; AStopWatch:TStopwatch; j:Int64; begin
  for i := 0 to 4 do //若是產生的Connection大於鏈接池的數量則會報錯提示
  begin ATrhed:= TDBThread.Create(self); end; FDConnection1.ConnectionDefName :='oracle' ; FDConnection1.LoginPrompt := false; //如下爲不用鏈接池,時間就比較長 AStopWatch := TStopwatch.StartNew; FDConnection1.Connected := true; AStopWatch.Stop; i:=AStopWatch.ElapsedTicks; self.Memo1.Lines.Add('not pooling: '+j.ToString()+' Ticks'); end; procedure TForm1.SpeedButton1Click(Sender: TObject); var conn: IFDStanConnectionDef; begin FDManager1.Close; conn:= self.FDManager1.ConnectionDefs.AddConnectionDef; conn.Name :='ora_test'; conn.Params.DriverID :='Ora'; conn.Params.UserName :='admin'; conn.Params.Password :='pwd'; conn.Params.Database := 'testdb'; conn.Params.Pooled := True; //啓用pool conn.Params.PoolMaximumItems := 5; //10.2.3中默認的最大池爲50.通常夠用 conn.Apply;//此方法能夠不用 FDManager1.Open;//產生池 end; { TDBThread }

constructor TDBThread.Create(AForm: TForm1); begin FreeOnTerminate := True; FForm := AForm; FConn := TFDConnection.Create(nil); FQry := TFDQuery.Create(nil); FQry.Connection := FConn; FConn.ConnectionDefName := AForm.FDManager1.ConnectionDefs.ConnectionDefByName('ora_test').Name;//自動調用池,至關於FDmanager.AcquireConnection方法。最好按此列調用 inherited Create(false);; end; destructor TDBThread.Destroy; begin FConn.Free; FQry.Free; inherited; end; procedure TDBThread.Execute; begin FQry.Close; FQry.Open('select sysdate from dual'); Synchronize(exePrc); end; procedure TDBThread.exePrc; var AStopWatch:TStopwatch; i:Int64; begin AStopWatch := TStopwatch.StartNew; FConn.Connected := True; AStopWatch.Stop; i:=AStopWatch.ElapsedTicks; FForm.Memo1.Lines.Add('pooling: '+i.ToString()+' Ticks'); end; end.

 運行結果:spa

相關文章
相關標籤/搜索