How to use the database connection pool to solve data connection problems

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.