如何采用数据库连接池解决数据连接问题

luckyboy97 2006-12-16 11:03:59
如何采用数据库连接池解决数据连接问题?欢迎大家一起讨论
...全文
282 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cjianwen 2006-12-29
  • 打赏
  • 举报
回复
要分
maxiang4460 2006-12-18
  • 打赏
  • 举报
回复
。。。

学习
  • 打赏
  • 举报
回复
学习
顺路来讨点分,哈哈
我---山西-newsoft
zcxc 2006-12-17
  • 打赏
  • 举报
回复
gz
tianbaobao 2006-12-16
  • 打赏
  • 举报
回复
关注!!
meirenismy 2006-12-16
  • 打赏
  • 举报
回复
up
cjianwen 2006-12-16
  • 打赏
  • 举报
回复
unit Unit2;

interface
uses
Contnrs, Classes, ADODB, Windows;

type
TConnnectionPool = class(TThread)
private
FConnStr: String;
FMaxConn: Integer;
FConnList: TThreadList;
FUnUseConn: TADOConnection;
procedure GetUnUseConn;
function GetCurrenCout: Integer;
protected
procedure Execute; override;
public
Constructor Create(ConnStr: String; maxConn: Integer = 20);
destructor Destroy; override;
function GetConnection: TADOConnection;
end;

implementation

{ TConnnectionPool }

constructor TConnnectionPool.Create(ConnStr: String; maxConn: Integer);
begin
FreeOnTerminate := True;
FConnStr := FConnStr;
FConnList := TThreadList.Create;
Inherited Create(False);
end;

destructor TConnnectionPool.Destroy;
var
i,count: Integer;
List: TList;
obj: TObject;
begin
List = FConnList.LockList;
if List.Count > 0 then
begin
for i := 0 to List.Count - 1 do
begin
obj := TObject(List.Items[i]);
Obj.Free;
end;
end;
FConnList.Free;
inherited;
end;

procedure TConnnectionPool.Execute;
var
aConn: TADOConnection;
i,count: Integer;
List: TList;
obj: TADOConnection;
begin
while not Terminated do
begin
Count := GetCurrenCout;
if Count < FMaxConn then
begin
aConn := TADOConnection.Create(nil);
aConn.ConnectionString := FConnStr;
aConn.Connected := True;
FConnList.Add(aConn);
end
else if count > FMaxConn then
begin
List := FConnList.LockList;
for i := 0 to List.Count - 1 do
begin
obj := TADOConnection(List.Items[i]);
if(obj.DataSetCount = 0) then
begin
FConnList.Remove(obj);;
obj.Free;
end;
end;
FConnList.UnlockList;
end;
GetUnUseConn;
sleep(50);
end;
end;

function TConnnectionPool.GetConnection: TADOConnection;
var
obj: TADOConnection;
begin
if Assigned(FUnUseConn) then
begin
Result := FUnUseConn;
Exit;
end;
obj := TADOConnection.Create(nil);
obj.ConnectionString := FConnStr;
obj.Connected := True;
FConnList.Add(obj);
Result := obj;
end;

function TConnnectionPool.GetCurrenCout: Integer;
begin
Result := FConnList.LockList.Count;
FConnList.UnlockList;
end;

procedure TConnnectionPool.GetUnUseConn;
var
count,i: Integer;
obj: TADOConnection;
List: TList;
begin
count := GetCurrenCout;
if count < 1 then
begin
FUnUseConn := nil;
Exit;
end;
List := FConnList.LockList;
for i := 0 to List.Count - 1 do
begin
obj := TADOConnection(List.Items[i]);
if(obj.DataSetCount = 0) then
begin
FUnUseConn := obj;
break;
end;
end;
FConnList.UnlockList;
end;

end.


不知道好不好用。。。

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧