打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SqlQuery池
unit SqlQueryPool;
interface
uses
  Classes, Windows, SysUtils, forms, DB, SqlExpr, FMTBcd;
type
  TSQLQueryPool = class(TObject)
  private
    FObjList:TThreadList;
    FTimeout: Integer;
    FMaxCount: Integer;
    FSemaphore: Cardinal;
    function CreateNewInstance(List:TList): TSQLQuery;
    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: TSQLQuery;
    procedure UnLock(var Value: TSQLQuery);
  end;
var
  DBXQryPool: TSQLQueryPool;
implementation
constructor TSQLQueryPool.Create(ACapicity:Integer=30);
begin
  FObjList:=TThreadList.Create;
  FTimeout := 3000;
  FMaxCount := ACapicity;
  FSemaphore := CreateSemaphore(nil, FMaxCount, FMaxCount, nil);   
end;
function TSQLQueryPool.CreateNewInstance(List:TList): TSQLQuery;
var
  p: TSQLQuery;
begin
  try
    p := TSQLQuery.Create(nil);
    p.Tag := 1;
    List.Add(p);
    Result := p;
  except
    Result := nil;
    Exit;
  end;
end;
destructor TSQLQueryPool.Destroy;
var
  i: Integer;
  List:TList;
begin
  List:=FObjList.LockList;
  try
    for i := List.Count - 1 downto 0 do
    begin
      TSQLQuery(List[i]).Free;
    end;
  finally
    FObjList.UnlockList;
  end;
  FObjList.Free;
  FObjList := nil;
  CloseHandle(FSemaphore);
  inherited Destroy;
end;
function TSQLQueryPool.GetLock(List:TList;Index: Integer): Boolean;
begin
  try
    Result := TSQLQuery(List[Index]).Tag = 0;
    if Result then
      TSQLQuery(List[Index]).Tag := 1;
  except
    Result :=False;
    Exit;
  end;
end;
function TSQLQueryPool.Lock: TSQLQuery;
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 := TSQLQuery(List[i]);
       //   PostMessage(Application.MainForm.Handle, 8888,23,0);
          Exit;
        end;
      end;
      if List.Count < MaxCount then
      begin
        Result := CreateNewInstance(List);
      //  PostMessage(Application.MainForm.Handle, 8888,21,0);
      end;
    finally
      FObjList.UnlockList;
    end;
  except
    Result :=nil;
    Exit;
  end;
end;
procedure TSQLQueryPool.Unlock(var Value: TSQLQuery);
var
  List:TList;
begin
  try
    List:=FObjList.LockList;
    try
      TSQLQuery(List[List.IndexOf(Value)]).Tag :=0;
      ReleaseSemaphore(FSemaphore, 1, nil);
    finally
      FObjList.UnlockList;
    end;
  //  PostMessage(Application.MainForm.Handle, 8888, 22, 0);
  except
    Exit;
  end;
end;
initialization
  DBXQryPool := TSQLQueryPool.Create();
finalization
  FreeAndNil(DBXQryPool);
end.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
分析 TStringList 存取对象的特点
StringsToPCharArray PCharArrayToStrings FreePCharArray
delphi的接口陷阱
delphi 版 sqlHelper第二版
Delphi 小谈之TList 篇
信息学竞赛→图片作品→查看图片→线性表的数组实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服