乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      簡單到不能再簡單的連接池

       delphiXE6 2014-05-06
      unit AdoconnectPool;

      interface

      uses
        Classes, Windows, SyncObjs, SysUtils, ADODB;

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

          constructor Create(ACapicity:Integer=15);overload;
          destructor Destroy;override;
          function LockConnection: TADOConnection;
          procedure UnlockConnection(var Value: TADOConnection);
        end;

      var
        ConnectionPool: TADOConnectionPool;

      implementation

      constructor TADOConnectionPool.Create(ACapicity:Integer=15);
      begin
        FObjList:=TThreadList.Create;
        FTimeout := 15000;
        FMaxCount := ACapicity;
        FSemaphore := CreateSemaphore(nil, FMaxCount, FMaxCount, nil);    
      end;

      function TADOConnectionPool.CreateNewInstance(List:TList): TADOConnection;
      var
        p: TADOConnection;
      begin
        try
          Result := nil;
          p := TADOConnection.Create(nil);
          p.ConnectionString := ConnectionString;
          p.LoginPrompt := False;
          p.Connected:=True;
          p.Tag := 1;
          List.Add(p);
          Result := p;
          p.Free;
        except
          Exit;
        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;
            Dispose(List[i]);
          end;
        finally
          FObjList.UnlockList;
        end;
        FObjList.Free;
        FObjList := nil;
        CloseHandle(FSemaphore);
        inherited Destroy;
      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
          Exit;
        end;
      end;

      function TADOConnectionPool.LockConnection: TADOConnection;
      var
        i,WaitResult: Integer;
        List:TList;
      begin
        try
          Result := nil;
          WaitResult:= WaitForSingleObject(FSemaphore, Timeout);
          List:=FObjList.LockList;
          try
            for i := 0 to List.Count - 1 do
            begin
              if GetLock(List,i) then
              begin
                Result := TADOConnection(List[i]);
                Exit;
              end;
            end;
            if List.Count < MaxCount then
              Result := CreateNewInstance(List);
          finally
            FObjList.UnlockList;
          end;
        except
          Exit;
        end;
      end;

      procedure TADOConnectionPool.UnlockConnection(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;
        except
          Exit;
        end;
      end;

      initialization
        ConnectionPool := TADOConnectionPool.Create();
      finalization
        ConnectionPool.Free;

      end.

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多