博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ADOConnection数据库连接池
阅读量:6807 次
发布时间:2019-06-26

本文共 2904 字,大约阅读时间需要 9 分钟。

unit AdoconnectPool;

interface

uses

  Classes, Windows, SysUtils, ADODB, IniFiles, forms;

type

  TADOConnectionPool = class(TObject)
  private
    FObjList:TThreadList;
    FTimeout: Integer;
    FMaxCount: Integer;
    FSemaphore: Cardinal;
    function CreateNewInstance(List:TList): TADOConnection;
    function GetLock(List:TList;Index: Integer): Boolean;
  public
    property Timeout:Integer read FTimeout write FTimeout;
    property MaxCount:Integer read FMaxCount;

    constructor Create(ACapicity:Integer=30);overload;

    destructor Destroy;override;
    function Lock: TADOConnection;
    procedure Unlock(var Value: TADOConnection);
  end;

var

  ConnPool: TADOConnectionPool;
  g_ini: TIniFile;

implementation

constructor TADOConnectionPool.Create(ACapicity:Integer=30);

begin
  FObjList:=TThreadList.Create;
  FTimeout := 3000;              // 3 second
  FMaxCount := ACapicity;
  FSemaphore := CreateSemaphore(nil, FMaxCount, FMaxCount, nil);
end;

function TADOConnectionPool.CreateNewInstance(List:TList): TADOConnection;

var
  p: TADOConnection;
 
  function GetConnStr: string;
  begin
    try
      Result := g_ini.ReadString('ado','connstr','');
    except
      Exit;
    end;
  end;
begin
  try
    p := TADOConnection.Create(nil);
    p.ConnectionString := GetConnStr;
    p.LoginPrompt := False;
    p.Connected:=True;
    p.Tag := 1;
    List.Add(p);
    Result := p;
  except
    on E: Exception do
    begin
      Result := nil;
      Exit;
    end;
  end;
end;

destructor TADOConnectionPool.Destroy;

var
  i: Integer;
  List:TList;
begin
  List:=FObjList.LockList;
  try
    for i := List.Count - 1 downto 0 do
    begin
      TADOConnection(List[i]).Free;
    end;
  finally
    FObjList.UnlockList;
  end;
  FObjList.Free;
  FObjList := nil;
  CloseHandle(FSemaphore);
  inherited;
end;

function TADOConnectionPool.GetLock(List:TList;Index: Integer): Boolean;

begin
  try
    Result := TADOConnection(List[Index]).Tag = 0;
    if Result then
      TADOConnection(List[Index]).Tag := 1;
  except
    Result :=False;
    Exit;
  end;
end;

function TADOConnectionPool.Lock: TADOConnection;

var
  i: Integer;
  List:TList;
begin
  try
    Result :=nil;
    if WaitForSingleObject(FSemaphore, Timeout) = WAIT_FAILED then Exit;
    List:=FObjList.LockList;
    try
      for i := 0 to List.Count - 1 do
      begin
        if GetLock(List,i) then
        begin
          Result := TADOConnection(List[i]);
          PostMessage(Application.MainForm.Handle,8888,13,0);
          Exit;
        end;
      end;
      if List.Count < MaxCount then
      begin
        Result := CreateNewInstance(List);
        PostMessage(Application.MainForm.Handle,8888,11,0);
      end;
    finally
      FObjList.UnlockList;
    end;
  except
    Result := nil;
    Exit;
  end;
end;

procedure TADOConnectionPool.Unlock(var Value: TADOConnection);

var
  List:TList;
begin
  try
    List:=FObjList.LockList;
    try
      TADOConnection(List[List.IndexOf(Value)]).Tag :=0;
      ReleaseSemaphore(FSemaphore, 1, nil);
    finally
      FObjList.UnlockList;
    end;
    PostMessage(Application.MainForm.Handle, 8888, 12, 0);
  except
    Exit;
  end;
end;

initialization

  ConnPool := TADOConnectionPool.Create();
  g_ini := TIniFile.Create(ExtractFilePath(Application.ExeName)+'server.ini');
finalization
  FreeAndNil(ConnPool);
  FreeAndNil(g_ini);

end.

 

转载地址:http://jitwl.baihongyu.com/

你可能感兴趣的文章
memcache集群服务:memagent配置使用
查看>>
备课好帮手,免费分享 下载
查看>>
mysql批量插入数据脚本
查看>>
MySQL索引类型总结和使用技巧以及注意事项(转)
查看>>
怎么把文件传到虚拟机里
查看>>
Java简单的下载程序
查看>>
外卖排序系统特征生产框架
查看>>
PowerShell提示输入命令不是有效命令
查看>>
写个APP
查看>>
一个不简洁的约瑟夫环解法
查看>>
Android文件的保存(本地和SD卡)与读取
查看>>
win7电脑桌面壁纸曝光过高影响图标怎么办?亲测实用解决方法
查看>>
Linux_防火墙入门01:以太网的诞生与演变历程
查看>>
修改微博的尾巴
查看>>
VS2010与.NET4系列 13. ASP.NET 4 SEO 改进
查看>>
远程桌面软件
查看>>
coreseek 利用python作数据源建立索引
查看>>
linux网卡绑定
查看>>
用P3P header解决iframe跨域访问cookie
查看>>
JS日期格式化
查看>>