当前位置: 首页 > news >正文

在Delphi中使用连接池连接MSSQL数据库和不使用连接池连接数据库的有什么区别

以下是完整的Delphi数据库连接池实现代码,包含初始化、连接获取和使用示例:

 

1. 连接池初始化单元 (uDBPool.pas)

 

unit uDBPool;interfaceusesFireDAC.Comp.Client, FireDAC.Stan.Def;procedure InitConnectionPool;
function GetConnectionFromPool: TFDConnection;
procedure ReleaseConnection(var AConnection: TFDConnection);implementationusesFireDAC.Phys.MSSQL;procedure InitConnectionPool;
beginFDManager.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;
beginResult := TFDConnection.Create(nil);tryResult.ConnectionDefName := 'MSSQL_Pool';Result.LoginPrompt := False;Result.ResourceOptions.AutoReconnect := True;Result.FetchOptions.Mode := fmAutomatic;Result.FetchOptions.AutoClose := True;Result.Open;exceptFreeAndNil(Result);raise;end;
end;procedure ReleaseConnection(var AConnection: TFDConnection);
beginif Assigned(AConnection) thenbeginif AConnection.Connected thenAConnection.Close;FreeAndNil(AConnection);end;
end;end.

 

2. 使用示例单元 (uMainForm.pas)

 

unit uMainForm;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,FireDAC.Comp.Client, Vcl.StdCtrls;typeTfrmMain = class(TForm)btnQuery: TButton;procedure FormCreate(Sender: TObject);procedure btnQueryClick(Sender: TObject);procedure FormDestroy(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varfrmMain: TfrmMain;implementation{$R *.dfm}uses uDBPool;procedure TfrmMain.FormCreate(Sender: TObject);
beginInitConnectionPool;
end;procedure TfrmMain.btnQueryClick(Sender: TObject);
varConn: TFDConnection;Query: TFDQuery;
beginConn := GetConnectionFromPool;tryQuery := TFDQuery.Create(nil);tryQuery.Connection := Conn;Query.SQL.Text := 'SELECT TOP 10 * FROM Customers';Query.Open;while not Query.Eof dobeginShowMessage(Query.FieldByName('CompanyName').AsString);Query.Next;end;finallyQuery.Free;end;finallyReleaseConnection(Conn);end;
end;procedure TfrmMain.FormDestroy(Sender: TObject);
beginFDManager.Close;
end;end.

 

3. 直接连接版本 (非池化)

 

unit uDirectDB;interfacefunction GetDirectConnection: TFDConnection;
procedure ReleaseDirectConnection(var AConnection: TFDConnection);implementationfunction GetDirectConnection: TFDConnection;
beginResult := TFDConnection.Create(nil);trywith Result.Params dobeginAdd('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;exceptFreeAndNil(Result);raise;end;
end;procedure ReleaseDirectConnection(var AConnection: TFDConnection);
beginif Assigned(AConnection) thenbeginif AConnection.Connected thenAConnection.Close;FreeAndNil(AConnection);end;
end;end.

 

4. 项目文件 (Project1.dpr)

 

program Project1;usesVcl.Forms,uMainForm in 'uMainForm.pas' {frmMain},uDBPool in 'uDBPool.pas',uDirectDB in 'uDirectDB.pas';{$R *.res}beginApplication.Initialize;Application.MainFormOnTaskbar := True;Application.CreateForm(TfrmMain, frmMain);Application.Run;
end.

 

关键功能说明

 

  1. 连接池管理
    • 自动维护连接生命周期
    • 支持最大20个连接
    • 30秒空闲超时
  2. 异常安全
    • 所有资源都有try-finally保护
    • 连接失败时自动释放资源
  3. 使用建议
    • 主窗体创建时初始化连接池
    • 每次查询获取新连接
    • 操作完成后立即释放
  4. 性能优化
    • 启用MARS(多活动结果集)
    • 自动重连机制
    • 智能数据获取模式

 

这套实现可以直接集成到现有Delphi项目中,只需修改连接字符串参数即可使用。

http://www.jsqmd.com/news/32410/

相关文章:

  • Go红队开发—图形化界面
  • 使用FireDAC的TFDManager和TFDConnection组件实现运行时动态数据库连接
  • laas paas saas 架构说明
  • Delphi RESTful API
  • TFDMemTable 是断开性数据集
  • 2025年河南公共走廊全钢防火隔断公司权威推荐榜单:商场全钢防火隔断/公共走廊防火隔断/公共走廊防火隔墙源头厂家精选
  • 2025年板式换热机组制造厂权威推荐榜单:容积式换热机组/热力机组/换热机组源头厂家精选
  • 十四.JavaScript 简介及导入方式
  • 2025 年 11 月音乐喷泉工程,景区互动音乐喷泉,彩色灯光音乐喷泉厂家最新推荐,聚焦资质、案例、售后的五家机构深度解读!
  • 11.5 NOIP 模拟赛 T1. 荣耀
  • 智能体自动化 ui 测试
  • Windows开机自动播放视频设置
  • 2025 年 11 月倍捻机,直捻机,大卷装倍捻机厂家最新推荐,实力品牌深度解析采购无忧之选!
  • 皮试
  • 使用rsync及inotify实现两台Linux设备间的文件夹同步
  • 教育部等七部门关于加强中小学科技教育的意见-解读
  • 2025年酒柜定制厂排名:酒柜定制选择哪家好?
  • 2025 年 11 月智能倍捻机,节能倍捻机,高速大卷装倍捻机厂家最新推荐,实力品牌深度解析采购无忧之选!
  • AT_arc188_d [ARC188D] Mirror and Order
  • 西门子实物图64 dq a0 B0排查
  • QwQ 32B VS DeepSeek R1
  • 2025 年最新反应釜生产厂家/推荐榜单:聚焦专业服务、市场口碑与权威测评的优质企业甄选指南搅拌/树脂/高速/远红外反应釜公司推荐
  • 2025 年最新冷凝器源头厂家推荐排行榜:聚焦定制化服务,助力企业精准选品不锈钢冷凝器/壳管式冷凝器/管壳式冷凝器公司推荐
  • 2025 年塑胶厂家最新推荐榜单:防水充电桩塑胶注塑及医疗塑胶制品优质厂商权威测评结果医疗塑胶结构件/医用塑胶配件/医用塑胶外壳公司推荐
  • 2025年汽车润滑油高性价比品牌推荐,上海德之星润滑油有限公司
  • 2025 年北戴河海鲜餐厅推荐权威榜单,聚焦专业采购与精湛厨艺的优质之选北戴河海鲜,北戴河特色美食店推荐
  • 2025 年 11 月氟碳喷涂精致钢厂家推荐排行榜,门窗精致钢,幕墙精致钢,装饰精致钢,定制精致钢公司推荐
  • 软件测试:边界值分析法详解
  • 2025 年 11 月社区养老院,老年痴呆养老院,自理老人住养老院最新推荐,聚焦资质、案例、售后的五家机构深度解读
  • Branching Heuristic Combining in SAT