在Delphi中使用連接池連接MSSQL數據庫和不使用連接池連接數據庫的有什么區別
以下是完整的Delphi數據庫連接池實現代碼,包含初始化、連接獲取和使用示例:
1. 連接池初始化單元 (uDBPool.pas)
unit uDBPool;
interface
uses
FireDAC.Comp.Client, FireDAC.Stan.Def;
procedure InitConnectionPool;
function GetConnectionFromPool: TFDConnection;
procedure ReleaseConnection(var AConnection: TFDConnection);
implementation
uses
FireDAC.Phys.MSSQL;
procedure InitConnectionPool;
begin
FDManager.Close;
FDManager.ConnectionDefs.Clear;
FDManager.AddConnectionDef('MSSQL_Pool', 'MSSQL',
'DriverID=MSSQL;' +
'Server=192.168.1.100,1433;' +
'Database=MyDB;' +
'User_Name=sa;' +
'Password=123456;' +
'Pooled=True;' +
'POOL_MaxSize=20;' +
'POOL_ExpireTimeout=30000;' +
'LoginTimeout=3;' +
'Mars=Yes');
FDManager.Active := True;
end;
function GetConnectionFromPool: TFDConnection;
begin
Result := TFDConnection.Create(nil);
try
Result.ConnectionDefName := 'MSSQL_Pool';
Result.LoginPrompt := False;
Result.ResourceOptions.AutoReconnect := True;
Result.FetchOptions.Mode := fmAutomatic;
Result.FetchOptions.AutoClose := True;
Result.Open;
except
FreeAndNil(Result);
raise;
end;
end;
procedure ReleaseConnection(var AConnection: TFDConnection);
begin
if Assigned(AConnection) then
begin
if AConnection.Connected then
AConnection.Close;
FreeAndNil(AConnection);
end;
end;
end.
2. 使用示例單元 (uMainForm.pas)
unit uMainForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
FireDAC.Comp.Client, Vcl.StdCtrls;
type
TfrmMain = class(TForm)
btnQuery: TButton;
procedure FormCreate(Sender: TObject);
procedure btnQueryClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
uses uDBPool;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
InitConnectionPool;
end;
procedure TfrmMain.btnQueryClick(Sender: TObject);
var
Conn: TFDConnection;
Query: TFDQuery;
begin
Conn := GetConnectionFromPool;
try
Query := TFDQuery.Create(nil);
try
Query.Connection := Conn;
Query.SQL.Text := 'SELECT TOP 10 * FROM Customers';
Query.Open;
while not Query.Eof do
begin
ShowMessage(Query.FieldByName('CompanyName').AsString);
Query.Next;
end;
finally
Query.Free;
end;
finally
ReleaseConnection(Conn);
end;
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
FDManager.Close;
end;
end.
3. 直接連接版本 (非池化)
unit uDirectDB;
interface
function GetDirectConnection: TFDConnection;
procedure ReleaseDirectConnection(var AConnection: TFDConnection);
implementation
function GetDirectConnection: TFDConnection;
begin
Result := TFDConnection.Create(nil);
try
with Result.Params do
begin
Add('DriverID=MSSQL');
Add('Server=192.168.1.100,1433');
Add('Database=MyDB');
Add('User_Name=sa');
Add('Password=123456');
Add('Pooled=False');
Add('LoginTimeout=3');
end;
Result.LoginPrompt := False;
Result.Open;
except
FreeAndNil(Result);
raise;
end;
end;
procedure ReleaseDirectConnection(var AConnection: TFDConnection);
begin
if Assigned(AConnection) then
begin
if AConnection.Connected then
AConnection.Close;
FreeAndNil(AConnection);
end;
end;
end.
4. 項目文件 (Project1.dpr)
program Project1;
uses
Vcl.Forms,
uMainForm in 'uMainForm.pas' {frmMain},
uDBPool in 'uDBPool.pas',
uDirectDB in 'uDirectDB.pas';
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end.
關鍵功能說明
- 連接池管理:
- 自動維護連接生命周期
- 支持最大20個連接
- 30秒空閑超時
- 異常安全:
- 所有資源都有try-finally保護
- 連接失敗時自動釋放資源
- 使用建議:
- 主窗體創建時初始化連接池
- 每次查詢獲取新連接
- 操作完成后立即釋放
- 性能優化:
- 啟用MARS(多活動結果集)
- 自動重連機制
- 智能數據獲取模式
這套實現可以直接集成到現有Delphi項目中,只需修改連接字符串參數即可使用。

浙公網安備 33010602011771號