使用 TFDConnection 的 pooled 连接池
从开始看到这个属性,就一直认为他可以提供一个连接池管理功能, 苦于文档资料太少, 甚至在帮助中对该属性的使用都没有任何介绍,如果你搜索百度,也会发现基本没资料。
最后终于在其官方网站看到了其完整相关的英文资料,虽然没有正面介绍该属性,但却是要启用该属性的详细方法:
Defining Connection (FireDAC)
A 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:
| Type | Description | Pros | Cons |
|---|---|---|---|
| Persistent | Has 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. |
| Private | Has 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. |
| Temporary | Has 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):
uses
FireDAC.Comp.Client, FireDAC.Stan.Intf;
var
oDef: IFDStanConnectionDef;
begin
oDef := 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:
var
oParams: TStrings;
begin
oParams := 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:
uses
FireDAC.VCLUI.ConnEdit;
...
if TfrmFDGUIxFormsConnEdit.Execute(FDConnection1, '') then
FDConnection1.Connected := True;
To edit a connection definition represented as a FireDAC connection string, use the code:
uses
FireDAC.VCLUI.ConnEdit;
...
var
sConnStr: String;
...
sConnStr := FDConnection1.ResultConnectionDef.BuildString();
if TfrmFDGUIxFormsConnEdit.Execute(sConnStr, '') then begin
FDConnection1.ResultConnectionDef.ParseString(sConnStr);
FDConnection1.Connected := True;
end;
使用 TFDConnection 的 pooled 连接池的更多相关文章
- Jar程序使用MyBatis集成阿里巴巴druid连接池
在写jar程序,而不是web程序的时候,使用mybatis作为持久层,可以集成POOLED连接池,而阿里的druid不能用,确实很郁闷.不过有办法. 首先准备好数据库配置文件 然后对Druid进行一个 ...
- Mybatis连接池及事务
一:Mybatis连接池 我们在学习WEB技术的时候肯定接触过许多连接池,比如C3P0.dbcp.druid,但是我们今天说的mybatis中也有连接池技术,可是它采用的是自己内部实现了一个连接池技术 ...
- FIREDAC FDConnection 连接池 连接串
一.FDConnection 连接池 http://docs.embarcadero.com/products/rad_studio/firedac/frames.html?frmname=topic ...
- 三句话搞定FireDAC连接池
form上拖入: FDManager1: TFDManager; FDConnection1: TFDConnection; //初始化连接池procedure TForm1.InitDBPool;b ...
- Delphi XE FireDac 连接池
在开发Datasnap三层中,使用FireDac 连接 MSSQL数据库. 实现过程如下: 1.在ServerMethods 单元中放入 FDManager.FDPhysMSSQLDriverLin ...
- Redis 连接池的问题
目录 Redis 连接池的问题 1 1. 前言 1 2.解决方法 1 前言 问题描述:Redis跑了一段时间之后,出现了以下异常. Redis Timeout ex ...
- 连接池和 "Timeout expired"异常
转自:博客园宁静.致远:http://www.cnblogs.com/zhangzhu/archive/2013/10/10/3361197.html 异常信息: MySql.Data.MySqlCl ...
- 连接池和 "Timeout expired"异常【转】
异常信息: MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout expired. The tim ...
- oracle database resident connection pooling(驻留连接池)
oracle在11g中引入了database resident connection pooling(DRCP).在此之前,我们可以使用dedicated 或者share 方式来链接数据库,dedic ...
随机推荐
- 配置SQL Server Session方法
以下过程是在Win 2003 SP2 + IIS 6.0, ASP.NET 2.0, SQL Server 2005下进行的. 1. 安装Session数据库到Framework目录 C:\WINDO ...
- Android PickerView滚动选择器的使用方法
手机里设置闹钟需要选择时间,那个选择时间的控件就是滚动选择器,前几天用手机刷了MIUI,发现自带的那个时间选择器效果挺好看的,于是就自己仿写了一个,权当练手.先来看效果: 效果还行吧?实现思路就是自定 ...
- BI如何让企业管理从信息化迈向智能化 ——暨珠海CIO协会成立大会圆满召开
2016年8月27日,珠海CIO协会成立大会在珠海度假村酒店成功举办.此次会议由奥威软件等数家公司共同协办.珠海市信息协会秘书长周德元先生.广东省首席信息官协会秘书长周庆林先生.珠海市首席信息官协会会 ...
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
本实例弹出窗口主要是继承PopupWindow类来实现的弹出窗体,布局可以根据自己定义设计.弹出效果主要使用了translate和alpha样式实现,具体实习如下: 第一步:设计弹出窗口xml: &l ...
- pymongo使用总结
0. 何为pymongo pymongo是操作MongoDB的python模块 1.安装pymongo # easy_install pymongo 2.连接mongodb >>> ...
- iOS符号表
https://docs.bugtags.com/zh/symbols/ios/find.html 发红包的限制 1.发送频率规则 ◆ 每分钟发送红包数量不得超过1800个: ◆ 同一个商户号,每分钟 ...
- 使用JavaScript闭包,以工厂模式实现定时器对象
原始对象写法 一般工作中写Javascript代码,主要写全局函数,并组织函数之间的调用,确实比较低级, 于是想利用面向对象的思想应用到JS编码中. 在火狐浏览器开发者网站上,看到一个实例利用对象技术 ...
- Javascript中Number()、parseIn()和parseFloat()的区别
有3个函数可以把非数值转化成数值:Number().parseInt()和parseFloat().第一个函数,即转型函数Number()可以用于任何数据类型,而另两个函数则专门用于把字符串转换成数值 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- WebService之Axis2 后续(6)~(10)目录
WebService大讲堂之Axis2(6):跨服务会话(Session)管理 WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService WebSe ...