使用 TFDConnection 的 pooled 连接池

article/2025/10/6 21:22:03

使用 TFDConnection 的 pooled 连接池

从开始看到这个属性,就一直认为他可以提供一个连接池管理功能, 苦于文档资料太少, 甚至在帮助中对该属性的使用都没有任何介绍,如果你搜索百度,也会发现基本没资料。

最后终于在其官方网站看到了其完整相关的英文资料,虽然没有正面介绍该属性,但却是要启用该属性的详细方法:

Defining Connection (FireDAC)

General

connection definition is a set of parameters that defines how to connect an application to a DBMS using a specific FireDAC driver. It is the equivalent of a BDE alias, ADO UDL (stored OLEDB connection string), or ODBC Data Source Name (DSN). For the list of supported database management systems and corresponding parameters, see FireDAC Database Connectivity.

FireDAC supports 3 connection definition kinds:

TypeDescriptionProsCons
PersistentHas a unique name, is managed by the FDManager, and is stored in a connection definition file.May be defined once and reused across many applications. May be pooled.The parameters (server address, DB name, and so on) are publicly visible and may be changed incidentally.

FDManager has to be reactivated or the Delphi IDE has to be restarted to make a newly added definition visible at design time.

PrivateHas a unique name, is managed by the FDManager, but is NOT stored in a connection definition file.Connection definition parameters are not visible "outside" the application. May be pooled.The application needs to create a private connection definition after each program restarts and cannot share it with the other programs.

Cannot be created at design time.

TemporaryHas no name, is not stored in a connection definition file, and is not managed by the FDManager.The simplest way to create a connection definition is to fill in the TFDConnection.Params property.

Can be created at design time using the TFDConnection component editor.

Similar to private. Also cannot be referenced by name and cannot be pooled.

Connection Definition File

The persistent connection definitions are stored in an external file - the connection definition file. This file has the standard INI text file format. It can be edited by FDExplorer or FDAdministrator utilities at first, manually, or by code. By default the file is C:\Users\Public\Documents\Embarcadero\Studio\14.0\FireDAC\FDConnectionDefs.ini.

Note: If you add a new persistent connection definition using FDExplorer or FDAdministrator while the RAD Studio IDE is running, it is not visible to the FireDAC design time code. To refresh the persistent connection definitions list, you need to reactivate FDManager or restart the RAD Studio IDE.

Sample content of this file:

[Oracle_Demo]
DriverID=Ora
Database=ORA_920_APP
User_Name=ADDemo
Password=a
MetaDefSchema=ADDemo
;MonitorBy=Remote[MSSQL_Demo]
DriverID=MSSQL
Server=127.0.0.1
Database=Northwind
User_Name=sa
Password=
MetaDefSchema=dbo
MetaDefCatalog=Northwind
MonitorBy=Remote

An application can specify a connection definition file name in the FDManager.ConnectionDefFileName property. FireDAC searches for a connection definition file in the following places:

  • If ConnectionDefFileName is specified:
    • search for a file name without a path, then look for it in an application EXE folder.
    • otherwise just use a specified file name.
  • If ConnectionDefFileName is not specified:
    • Look for FDConnectionDefs.ini in an application EXE folder.
    • If the file above is not found, look for a file specified in the registry key HKCU\Software\Embarcadero\FireDAC\ConnectionDefFile. By default it is C:\Users\Public\Documents\Embarcadero\Studio\14.0\FireDAC\FDConnectionDefs.ini.
Note: At design time, FireDAC ignores the value of the  FDManager.ConnectionDefFileName, and looks for a file in a RAD Studio Bin folder or as specified in the registry. If the file is not found, an exception is raised.

If FDManager.ConnectionDefFileAutoLoad is True, a connection definition file loads automatically. Otherwise, it must be loaded explicitly by calling the FDManager.LoadConnectionDefFile method before the first usage of the connection definitions. For example, before setting TFDConnection.Connected to True.

Creating a Persistent Connection Definition

A persistent connection definition can be created using FDExplorer or FDAdministrator. Here is how you can do that in code. Also see the demo FireDAC\Samples\Comp Layer\TFDConnection\ConnectionDefs.

The following code snippet creates a connection definition named "MSSQL_Connection", which has all parameters required to connect to the Microsoft SQL Server running locally, using the OS authentication (SSPI):

usesFireDAC.Comp.Client, FireDAC.Stan.Intf;
varoDef: IFDStanConnectionDef;
beginoDef := FDManager.ConnectionDefs.AddConnectionDef;oDef.Name := 'MSSQL_Connection';oDef.DriverID := 'MSSQL';oDef.Server := '127.0.0.1';oDef.Database := 'Northwind';oDef.OSAuthent := True;oDef.MarkPersistent;oDef.Apply;.....................FDConnection1.ConnectionDefName := 'MSSQL_Connection';FDConnection1.Connected := True;

FDManager is a global instance of the FireDAC connection manager. Its property FDManager.ConnectionDefs: IFDStanConnectionDefs is a collection of the persistent and private connection definitions. The AddConnectionDef method adds a new connection definition. The MarkPersistent method marks a connection definition as persistent. The Apply method saves a connection definition to a connection definition file. Without the MarkPersistent call, the connection definition is private.

Creating a Private Connection Definition

A private connection definition can be created only in code. The code is similar to the one above, but without the MarkPersistent call.

Also, you can use a technique similar to BDE:

varoParams: TStrings;
beginoParams := TStringList.Create;oParams.Add('Server=127.0.0.1');oParams.Add('Database=Northwind');oParams.Add('OSAuthent=Yes');FDManager.AddConnectionDef('MSSQL_Connection', 'MSSQL', oParams);.....................FDConnection1.ConnectionDefName := 'MSSQL_Connection';FDConnection1.Connected := True;

Creating a Temporary Connection Definition

A temporary connection definition can be created at design time using the FireDAC Connection Editor. In order to do this, double-click a TFDConnection to invoke the editor:

Or at run time in code by filling the TFDConnection.Params property. This is the simplest way to create a connection definition.

FDConnection1.DriverName := 'MSSQL';
FDConnection1.Params.Add('Server=127.0.0.1');
FDConnection1.Params.Add('Database=Northwind');
FDConnection1.Params.Add('User_name=sa');
FDConnection1.Connected := True;

Another option is to specify a connection string at run time by filling the TFDConnection.ConnectionString property. A connection string may be a convenient way to specify connection definition parameters for certain types of applications. For example:

FDConnection1.ConnectionString := 'DriverID=MSSQL;Server=127.0.0.1;Database=Northwind;User_name=sa';
FDConnection1.Connected := True;

Editing a Connection Definition

An application may need an ability to create and edit a connection definition at run time using standard FireDAC Connection Editor dialog. To edit a temporary connection definition stored in TFDConnection, use the code:

usesFireDAC.VCLUI.ConnEdit;
...
if TfrmFDGUIxFormsConnEdit.Execute(FDConnection1, '') thenFDConnection1.Connected := True;

To edit a connection definition represented as a FireDAC connection string, use the code:

usesFireDAC.VCLUI.ConnEdit;
...
varsConnStr: String;
...
sConnStr := FDConnection1.ResultConnectionDef.BuildString();
if TfrmFDGUIxFormsConnEdit.Execute(sConnStr, '') then beginFDConnection1.ResultConnectionDef.ParseString(sConnStr);FDConnection1.Connected := True;
end;

 


http://chatgpt.dhexx.cn/article/aZnZ71Ao.shtml

相关文章

Mybatis 连接池POOLED

1、连接池: 我们在实际开发中都会使用连接池。 因为它可以减少我们获取连接所消耗的时间。 2、mybatis中的连接池 在 Mybatis 中也有连接池技术,但是它采用的是自己的连接池技术。 在 Mybatis 的 SqlMapConfig.xml 配置文件中,通过来实 现 My…

Mybatis连接池介绍与分类 Mybatis使用POOLED UNPOOLED配置连接池的原理分析

一、连接池 1.概念:其实就是一个容器(集合),存放数据库连接的容器。 当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象&#xf…

阶段3 1.Mybatis_07.Mybatis的连接池及事务_3 mybatis连接池的分类

2、mybatis中的连接池 mybatis连接池提供了3种方式的配置: 配置的位置: 主配置文件SqlMapConfig.xml中的dataSource标签,type属性就是表示采用何种连接池方式。 type属性的取值: POO…

Monkey Test简单介绍

什么是Monkey Test?顾名思义,就像一只猴子一样,它的下一步具有随机性。所以Monkey Test可以简单地理解为动作随机性测试! Monkey是android模拟器或设备上运行的一个程序。它可以生成伪随机用户事件(例如点击、触碰或滑…

monkey工具详解

第一部分:背景 1.为什么要开展压力测试? 提高产品的稳定性 提高产品的留存率 2.什么时候开始压力测试? 首轮功能测试通过后 下班后的夜间进行 第二部分:理论 1.什么是monkey? Monkey是发送伪随机用户事件的工…

monkey的基本使用

一、monkey介绍 1.功能:采用伪随机测试的方式,来完成app的稳定性测试 2.执行原理:通过monkey的shell脚本去执行安卓系统中自带的monkey.jar工具 3.通常用于盲测,压力测试和冒烟测试 1)盲测:忽略功能和业务逻…

Monkey使用详解

App monkey 使用篇 安装包下载: 下载mumu模拟器地址:http://mumu.163.com/baidu/ 下载adb安装包 地 址:http://www.downza.cn/soft/219906.html Adb环境变量 配 置:在path里新建adb安装路径即可。 cmd 打开命令提示符窗口输入&a…

monkey的基本定义及基本使用(菜鸟学习中)

一.monkey的定义 1.(转自 https://blog.csdn.net/beyond_f/article/details/78543070 ) Monkey程序由Android系统自带,使用Java语言写成,在Android文件系统中的存放路径是:/system/framework/monkey.jar; Monkey.jar程序是由一…

Android测试--monkey详细到炸的总结

一、Monkey简介: Monkey是Android中的一个命令行工具,可以运行在模拟器里或者现实设备中,向系统发送伪随机的用户事件流(点击、滑动、Application切换、横竖屏、应用关闭)实现对正在开发的应用程序进行压力测试。monk…

APP稳定性测试利器 Monkey介绍、实战使用、日志分析

第一、Monkey简介 Monkey是什么? Monkey 是安卓官方提供的一个命令行工具,可以运行在Android模拟器和实体手机上。通过Monkey 来模拟用户的触摸、点击、滑动、系统按键的操作,来对APP进行压力测试、稳定性测试。换句话说,就是在乱…

Clumsy弱网、丢包测试工具

一、下载clumsy安装包,解压后打开clumsy.exe 二、ping www.baidu.com查看是否模拟成功 三、pc端模拟丢包和网络延迟 四、APP端模拟丢包和网络延迟 1.手机连接抓包工具charles 2.打开clumsy,在过滤器filtering中输入outbound and ip.DstAddr 192.168.…

【弱网】clumsy的filter语法设置

clumsy 官方说明 jagt WinDivert 的语法 https://github.com/basil00/Divert/wiki/WinDivert-Documentation#7-filter-languageDivert大神们的改版 clumsy-regoutbound 发送 inbound

弱网测试工具clumsy

clumsy 能在 Windows 平台下人工造成不稳定的网络状况,方便你调试应用程序在极端网络状况下的表现。 简介 利用封装 Winodws Filtering Platform 的WinDivert 库, clumsy 能实时的将系统接收和发出的网络数据包拦截下来,人工的造成延迟,掉包…

使用clumsy模拟网络延迟

为何模拟网络延迟 由于最近打算做及时对战类的游戏,对于及时对战类的游戏,首先要解决的问题就是网络延迟的问题。网络延迟对于游戏的体验至关重要。那么开发一款网络游戏要怎么解决这一问题呢。想要解决网络延迟,首先就要模拟网络延迟。那么…

[测试]Clumsy网络模拟工具

笔者最近因为需要在局域网内模拟一个实际情况(即存在丢包、延迟等情况)。网上搜了很多工具,大多基于linux。后来好不容易在网上搜索到一款2014年出现的网络模拟工具----Clumsy。 (参考了http://jagt.github.io/clumsy/&#xff09…

curle(curley)

Queen Elizabeth is afraid of Mary. Bess Curle wrote the story. 这两句什么意思? 伊丽莎白女王害怕玛丽,Bess Curle写(说)出了这个道理。(这里的story成为道理,事实意义,不再只是故事的意思),希望对你有帮助&#x…

网络抖动工具clumsy

网络抖动工具clumsy 下载与安装 下载地址:https://download.csdn.net/download/Asia1752/85192646 使用 说明: 1、Lag(延迟),把数据包缓存一段时间后再发出,这样能够模拟网络延迟的状况。 2、Drop(掉包),随机丢弃一…

网络丢包工具clumsy

以webrtc为例,未开始前 设置丢包率10%后,丢包会增加,如下图: Lag是延迟。设置后,增加网络延迟

Windows下网络环境模拟工具-Clumsy

公司设备需要模拟在弱网(如:延迟、丢包)环境下进行数据的传输,网上看资料找到一款工具Clumsy( 文章出处:Clumsy-Windows下网络环境模拟工具_clumsy工具_Hello,C!的博客-CSDN博客),安装进行模拟,基本可以满足…

弱网测试工具 clumsy

clumsy工具介绍 clumsy 能在 Windows 平台下人工造成不稳定的网络状况,方便你调试应用程序在极端网络状况下的表现。下面根据工作中遇到过的实际场景介绍一下clumsy工具的使用方法。 下载安装:clumsy下载链接 场景:测试客户端下载文件失败&…