How to use the database connection pool to solve data connection problems Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiDB/html/delphi_20061216110359275.html
How does one use a database connection pool to solve the data connection problem? Welcome to discuss
Up
Follow up !!
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.
I don't know...
GZ
...
Learning
Learning
Sorry, haha.
I --- Shanxi-newsoft
Minute