关于MIDAS中数据库连接池的实现
来源:互联网 发布:九星照命解法和算法 编辑:程序博客网 时间:2024/06/03 09:14
面是我的连接池的代码,我比较简单,还没有做计时释放的工作。
constructor TConnectionPools.Create;
begin
FConnList := TList.Create;
FCriticalSection := TCriticalSection.Create;
FTimeout := 5000;
FMaxCount := 15;
FSemaphore := CreateSemaphore(nil, FMaxCount, FMaxCount, nil);
end;
function TConnectionPools.CreateNewInstance: TADOConnection;
var
p: PRemoteConnection;
begin
Result := nil;
FCriticalSection.Enter;
try
New(p);
p.Connection := TADOConnection.Create(nil);
p.Connection.ConnectionString := ConnectionString;
p.Connection.LoginPrompt := False;
try
p.Connection.Open(DataBaseUser,DataBasePass);
except
p.Connection.Free;
Dispose(p);
Exit;
end;
p.InUse := True;
FConnList.Add(p);
Result := p.Connection;
finally
FCriticalSection.Leave;
end;
end;
destructor TConnectionPools.Destroy;
var
i: Integer;
begin
FCriticalSection.Free;
for i := 0 to FConnList.Count - 1 do
begin
PRemoteConnection(FConnList[i]).Connection.Free;
Dispose(FConnList[i]);
end;
FConnList.Free;
CloseHandle(FSemaphore);
inherited Destroy;
end;
function TConnectionPools.GetLock(Index: Integer): Boolean;
begin
FCriticalSection.Enter;
try
Result := not PRemoteConnection(FConnList[Index]).InUse;
if Result then
PRemoteConnection(FConnList[Index]).InUse := True;
finally
FCriticalSection.Leave;
end;
end;
function TConnectionPools.LockConnection: TADOConnection;
var
i: Integer;
begin
Result := nil;
if WaitForSingleObject(FSemaphore, Timeout) = WAIT_FAILED then
raise Exception.Create('服务器忙,请稍候再试');
for i := 0 to FConnList.Count - 1 do
begin
if GetLock(i) then
begin
Result := PRemoteConnection(FConnList[i]).Connection;
Exit;
end;
end;
if FConnList.Count < MaxCount then
Result := CreateNewInstance;
if Result = nil then { This shouldn't happen because of the sempahore locks }
raise Exception.Create('Unable to lock Connection');
end;
procedure TConnectionPools.ReleaseLock(Index: Integer;
var Value: TADOConnection);
begin
FCriticalSection.Enter;
try
PRemoteConnection(FConnList[Index]).InUse := False;
//Value := nil;
ReleaseSemaphore(FSemaphore, 1, nil);
finally
FCriticalSection.Leave;
end;
end;
procedure TConnectionPools.SetConnectionString(const Value: string);
begin
FConnectionString := Value;
end;
procedure TConnectionPools.SetDataBasePass(const Value: string);
begin
FDataBasePass := Value;
end;
procedure TConnectionPools.SetDataBaseUser(const Value: string);
begin
FDataBaseUser := Value;
end;
procedure TConnectionPools.UnlockConnection(var Value: TADOConnection);
var
i: Integer;
begin
for i := 0 to FConnList.Count - 1 do
begin
if Value = PRemoteConnection(FConnList[i]).Connection then
begin
ReleaseLock(i, Value);
break;
end;
end;
end;
initialization
ConnectionPools := TConnectionPools.Create;
finalization
ConnectionPools.Free;
end.
constructor TConnectionPools.Create;
begin
FConnList := TList.Create;
FCriticalSection := TCriticalSection.Create;
FTimeout := 5000;
FMaxCount := 15;
FSemaphore := CreateSemaphore(nil, FMaxCount, FMaxCount, nil);
end;
function TConnectionPools.CreateNewInstance: TADOConnection;
var
p: PRemoteConnection;
begin
Result := nil;
FCriticalSection.Enter;
try
New(p);
p.Connection := TADOConnection.Create(nil);
p.Connection.ConnectionString := ConnectionString;
p.Connection.LoginPrompt := False;
try
p.Connection.Open(DataBaseUser,DataBasePass);
except
p.Connection.Free;
Dispose(p);
Exit;
end;
p.InUse := True;
FConnList.Add(p);
Result := p.Connection;
finally
FCriticalSection.Leave;
end;
end;
destructor TConnectionPools.Destroy;
var
i: Integer;
begin
FCriticalSection.Free;
for i := 0 to FConnList.Count - 1 do
begin
PRemoteConnection(FConnList[i]).Connection.Free;
Dispose(FConnList[i]);
end;
FConnList.Free;
CloseHandle(FSemaphore);
inherited Destroy;
end;
function TConnectionPools.GetLock(Index: Integer): Boolean;
begin
FCriticalSection.Enter;
try
Result := not PRemoteConnection(FConnList[Index]).InUse;
if Result then
PRemoteConnection(FConnList[Index]).InUse := True;
finally
FCriticalSection.Leave;
end;
end;
function TConnectionPools.LockConnection: TADOConnection;
var
i: Integer;
begin
Result := nil;
if WaitForSingleObject(FSemaphore, Timeout) = WAIT_FAILED then
raise Exception.Create('服务器忙,请稍候再试');
for i := 0 to FConnList.Count - 1 do
begin
if GetLock(i) then
begin
Result := PRemoteConnection(FConnList[i]).Connection;
Exit;
end;
end;
if FConnList.Count < MaxCount then
Result := CreateNewInstance;
if Result = nil then { This shouldn't happen because of the sempahore locks }
raise Exception.Create('Unable to lock Connection');
end;
procedure TConnectionPools.ReleaseLock(Index: Integer;
var Value: TADOConnection);
begin
FCriticalSection.Enter;
try
PRemoteConnection(FConnList[Index]).InUse := False;
//Value := nil;
ReleaseSemaphore(FSemaphore, 1, nil);
finally
FCriticalSection.Leave;
end;
end;
procedure TConnectionPools.SetConnectionString(const Value: string);
begin
FConnectionString := Value;
end;
procedure TConnectionPools.SetDataBasePass(const Value: string);
begin
FDataBasePass := Value;
end;
procedure TConnectionPools.SetDataBaseUser(const Value: string);
begin
FDataBaseUser := Value;
end;
procedure TConnectionPools.UnlockConnection(var Value: TADOConnection);
var
i: Integer;
begin
for i := 0 to FConnList.Count - 1 do
begin
if Value = PRemoteConnection(FConnList[i]).Connection then
begin
ReleaseLock(i, Value);
break;
end;
end;
end;
initialization
ConnectionPools := TConnectionPools.Create;
finalization
ConnectionPools.Free;
end.
- 关于MIDAS中数据库连接池的实现
- 关于MIDAS.DLL 在三层架构中出现的问题
- 关于几中数据库连接池的比较
- JAVA中数据库连接池的动态实现
- Delphi中MIDAS的全面学习
- Delphi中MIDAS的全面学习
- Delphi中MIDAS程序的实例化模型种类(MIDAS之三)
- 在 C++ BUILDER 中开发MIDAS应用而不需要发布MIDAS .DLL 的方法
- Delphi中MIDAS是什么(MIDAS之一)
- 关于数据库连接池的方方面面
- 关于数据库连接池的研究
- java中关于数据库连接的获取
- 关于android中数据库连接的问题
- 在MIDAS中使用SocketConnection遇到的问题
- DataSnap(MIDAS)三层架构编程中,DataSetProvider的Option属性
- 数据库连接池的简单实现
- 实现高效的数据库连接池
- 数据库连接池的简单实现
- linux驱动阅读笔记
- 进对公司,入错部门
- 关于cvs
- MS-VC vs. Linux-IDE
- 使用DataList分页显示数据,双向排序,添加checkbox控件
- 关于MIDAS中数据库连接池的实现
- 哈哈我的blog终于有了,哈哈!
- 编程其实就是一个不断做出选择的过程
- 使用DataList数据修改,保存,取消
- c#.net常用函数列表
- GTK中的字符串操作GString
- 心理年龄鉴定
- GTK 计时器函数
- (GTK)错误处理及其他实用函数