ADO连接池

来源:互联网 发布:做淘宝亏本冲销量 编辑:程序博客网 时间:2024/03/29 01:05

ADO连接池
unit unADOConnectionPool;

interface
uses
  Windows,ADODB;

const
  MAX_CONNECTION_COUNT=5;
type

  TADOConnectionPool=class
  private
//    _CirticalSection:TCriticalSection;
    _ConnectionCount:integer;
    _ADOConnections:array of TADOConnection;
    _Mutexs:array of THandle;
  protected
    function FindConnectionIndex(Connection:TADoconnection):Integer ;
  public
    constructor Create(ConnectionCount:Integer=MAX_CONNECTION_COUNT);
    destructor Destroy; override;
    function InitConnection(ConnectionString:string):Integer;
    function GetConnection():TADoconnection ;
    procedure FreeConnection(Connection:TADoconnection);
  end;


  function GetADOConnectionPool():TADOConnectionPool ;
implementation

var
  ADOConnectionPool:TADOConnectionPool=nil;

function GetADOConnectionPool():TADOConnectionPool ;
begin
  if not Assigned(ADOConnectionPool) then
    ADOConnectionPool:=TADOConnectionPool.Create();
  Result:=ADOConnectionPool;
end;

{ TADOConnectionPool }


constructor TADOConnectionPool.Create(ConnectionCount:Integer);
var
  i:integer;
begin
//  _CirticalSection:=TCriticalSection.Create;
  _ConnectionCount:= ConnectionCount;

  SetLength(_ADOConnections,_ConnectionCount);
  SetLength(_Mutexs,_ConnectionCount);
  for i:=Low(_Mutexs) to High(_Mutexs) do
  begin
    _Mutexs[i]:=CreateMutex(nil,False,'');

  end;
end;

destructor TADOConnectionPool.Destroy;
var
  i:integer;
begin
  for i:=Low(_Mutexs) to High(_Mutexs) do
  begin
    CloseHandle( _Mutexs[i]);
  end;

  for i:=Low(_ADOConnections) to High(_ADOConnections) do
  begin
    _ADOConnections[i].Free;
  end;
//  _CirticalSection.Free;
  inherited;
end;

function TADOConnectionPool.FindConnectionIndex(
  Connection: TADoconnection): Integer;
var
  i:integer;
begin
  Result:=-1;
  for i:=Low(_ADOConnections) to High(_ADOConnections) do
  begin
    if Connection= _ADOConnections[i] then
    begin
      Result:=i;
      break;
    end;
  end;
end;

procedure TADOConnectionPool.FreeConnection(Connection:TADoconnection);
var
  iFindIndex:integer;
begin
  iFindIndex:=FindConnectionIndex(Connection);
  if 0<=iFindIndex then
   ReleaseMutex(_Mutexs[iFindIndex]);
end;

function TADOConnectionPool.GetConnection: TADoconnection;
var
  iConnectionIndex:integer;
begin
  Result:=nil;
//  _CirticalSection.Enter;
  iConnectionIndex:=WaitForMultipleObjects(_ConnectionCount,@_Mutexs[0],False,INFINITE) -WAIT_OBJECT_0 ;
  //OutputDebugString( PChar(Format(' ----%d ',[iConnectionIndex])));
  if iConnectionIndex<_ConnectionCount then
    Result:=_ADOConnections[iConnectionIndex];
//  _CirticalSection.Leave;
end;

function TADOConnectionPool.InitConnection(ConnectionString: string):Integer;
var
  i:integer;
begin
  Result:=0;

  for i:=Low(_ADOConnections) to High(_ADOConnections) do
  begin
    _ADOConnections[i]:=TADOConnection.Create(nil);
    try
      _ADOConnections[i].LoginPrompt:=False;
      _ADOConnections[i].ConnectionString:=ConnectionString;
      _ADOConnections[i].Connected:=True;
    except
      Result:=-1;
    end;
  end;

end;

initialization

finalization
  ADOConnectionPool.Free;
end.


测试中学习到的问题

1、    _Mutexs[i]:=CreateMutex(nil,False,'');
在创建的线程中,无论Mutex 是否在信号状态(也就是 第二个参数是True或) 创建线程调用WaitForMultipleObjects 都会成功

2、当拥有Mutex的线程没有ReleaseMutex就结束了,在WaitForMultipleObjects 的线程将返回WAIT_ABANDONED_0 ,标号是拥有Mutex的标号

原创粉丝点击