ado连接池

//==============================================================================
// ADO连接池         cxg                  2008-09-26 16:47:40
//==============================================================================

unit uADOPool;

interface

uses
  SysUtils,Classes,ADODB,Dialogs,IniFiles,Forms;

type// 数据库类型
  TDBType=(Access,SqlServer,Oracle);

type// 记录每个TADOConnection对象的状态
  Tado=record
    FName:string;
    FUsed:Boolean;             // 此TADOConnection对象是否已被使用
    P:TADOConnection;
  end;

// 动态生成n个TADOConnection并保存进对象池中
procedure ADOPool(Owner:TComponent;DBType:TDBType;MaxNums:Integer);
// 从连接池里取空闲TADOConnection
function GetADOConnection:TADOConnection;
// 使用完后使TADOConnection空闲出来
procedure SetNonUsed(AdoConnection:TADOConnection);

var
  ADOArray:array of Tado;        // 连接池容器

implementation

// 动态生成N个TADOConnection并保存进对象池中
// MaxNums 最大的连接对象数量
procedure ADOPool(Owner:TComponent;DBType:TDBType;MaxNums:Integer);
var
  i:Integer;
  Conn:TADOConnection;
  sFileName,sSection:string;
  ini:TIniFile;
begin
  sFileName:=ExtractFilePath(Application.ExeName)+'db.ini';
  SetLength(ADOArray,MaxNums);
 
  for i:=Low(ADOArray) to High(ADOArray) do
  begin
    with ADOArray[i] do
    begin
      FName:='con'+inttostr(i);
      FUsed:=False;

      Conn:=TADOConnection.Create(Owner);
      with Conn do
      begin
        p:=Conn;
        Conn.Name:=FName;
        LoginPrompt:=False;
        ini:=TIniFile.Create(sFileName);
        try
          case DBType of
            sqlserver:
            begin
              sSection:='sql server';
              Provider:=ini.ReadString(sSection,'provider','');
              Properties['Data Source'].Value:=ini.ReadString(sSection,'server','');
              Properties['User ID'].Value:=ini.ReadString(sSection,'userid','');
              Properties['Password'].Value:=ini.ReadString(sSection,'password','');
              Properties['Initial Catalog'].Value:=ini.ReadString(sSection,'database','');
            end;

            access:               
            begin
              sSection:='access';
              Provider:=ini.ReadString(sSection,'provider','');
              Properties['Jet OLEDB:Database Password'].Value:=ini.ReadString(sSection,'password','');
              Properties['Data Source'].Value:=ini.ReadString(sSection,'server','');
              Properties['User ID'].Value:=ini.ReadString(sSection,'userid','');
              Properties['Password'].Value:=ini.ReadString(sSection,'password','');
            end;

            oracle:
            begin
              sSection:='oracle';
              Provider:=ini.ReadString(sSection,'provider','');
              Properties['Data Source'].Value:=ini.ReadString(sSection,'server','');
              Properties['User ID'].Value:=ini.ReadString(sSection,'userid','');
              Properties['Password'].Value:=ini.ReadString(sSection,'password','');
            end;
          end;
         
          try
            Connected:=True;
          except
            raise Exception.Create('数据库连接失败');
          end;
        finally
          ini.Free;
        end;
      end;
    end;
  end;
end;

function GetADOConnection:TADOConnection;
var
  i,t:Integer;
begin
  t:=0;
  for i:=Low(ADOArray) to High(ADOArray) do
  begin
    if not ADOArray[i].FUsed then
    begin
      ADOArray[i].FUsed:=True;
      Result:=ADOArray[i].P;
      Inc(t);
      Break;
    end;
  end;
  if t=0 then ShowMessage('没有空闲的连接');
end;

procedure SetNonUsed(AdoConnection:TADOConnection);
var
  i:Integer;
begin
  for i:=Low(ADOArray) to High(ADOArray) do
  begin
    if ADOArray[i].FName=AdoConnection.Name then
    begin
      ADOArray[i].FUsed:=False;
      Break;
    end; 
  end; 
end;  

end.

原文地址:https://www.cnblogs.com/hnxxcxg/p/2940814.html