zoukankan      html  css  js  c++  java
  • FireDac Pooling

    1.建立FDManager的ConnectionDef。并设置此Pooling为True. 

    2.建立Thread类进行多个FDConnection连接DB。 

    3.本列是oracle远程数据.如下图:

    Open pooling是建立FDManger中的Connection. Thread按钮是建立连接的测试。右边的空白是显示连接时间(为Ticks)

    时间单位换算:

    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.

     运行结果:

     
  • 相关阅读:
    OpenFire源码学习之十九:在openfire中使用redis插件(上)
    OpenFire源码学习之十八:IOS离线推送
    OpenFire源码学习之十七:HTTP Service插件
    OpenFire源码学习之十六:wildfire
    OpenFire源码学习之十五:插件开发
    OpenFire源码学习之十四:插件管理
    OpenFire源码学习之十三:消息处理
    数据挖掘入门
    iOS小技巧
    图片加载完成之前对图片高度侦测
  • 原文地址:https://www.cnblogs.com/yagzh2000/p/9183110.html
Copyright © 2011-2022 走看看