使用 TFDConnection 的 pooled 连接池

转载 2015年07月09日 21:08:05
使用 TFDConnection 的 pooled 连接池

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

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

Defining Connection (FireDAC)

General

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:

Defining.png

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;

相关文章推荐

Delphi 10 Seattle FireDAC 连接池配置过程

一直想用Delphi 10 Seattle FireDAC 写个中间层,当然不得不解决连接池的问题。 网上搜到一个只有一点代码的例子,没有配置步骤,研究了半天的英文帮助文档,总算试验成功,给苦无资料...

(FireDAC) 连接定义

Defining Connection (FireDAC) 连接定义就是应用程序使用特定的FireDAC驱动连接数据库的参数集合。相当于BDE的别名,ADO的UDL,或者ODBC的DSN。 For t...

FIREDAC连接池配置

以下在delphi XE7下测试 fdphysrcldrvrlnk1: TFDPhysOracleDriverLink; fdgxwtcrsr1: TFDGUIxWaitCu...
  • rznice
  • rznice
  • 2014年11月25日 18:04
  • 6862

datasnap越用越慢原因

DataSnap Server DSServer1Disconnect 这个函数什么时候执行? void __fastcall TServerContainer1::DSServer...

一个例子说明如何在DataSnap中使用FireDAC

一、FIREDAC调用DataSnap远程方法查询数据示例 1、服务端使用FDQUERY查询数据并返回TDATASET: Function TServerMethods1.GetData(var sq...
  • xieyunc
  • xieyunc
  • 2015年09月10日 21:52
  • 5280

用FireDAC连接oracle

环境:delphi XE7。OS:Win8.1 UP1 64bit  DATABASE:oracle 64bi
  • rznice
  • rznice
  • 2014年09月22日 18:00
  • 4469

json中包含Unicode转码问题

今天在解析json数据的时候得到了一堆这样的数据:{"errNum":0,"errMsg":"success","retData":[{"title":"\u6536\u5e9f\u54c1\u592...
  • zml_2015
  • zml_2015
  • 2016年03月24日 15:03
  • 20909

RAD Studio 10 自带Demo代码汇总说明

大家好,好多朋友来信咨询Delphi和C++Builder的移动开发、DataSnap架构等问题,希望能有Demo代码学习。其实Delphi和C++Builder本身自带有很多示例代码,已经覆盖了大部...

终于可以发布Delphi下收点OSGI精髓皮毛而设计的插件框架WisdomPluginFramework

终于可以发布Delphi下收点OSGI精髓皮毛而设计的插件框架WisdomPluginFramework这是一个Delphi实现的插件框架, 我耗费了相当相当相当多的精力来设计她,虽然仅闪着点我微薄...
  • xieyunc
  • xieyunc
  • 2017年03月27日 16:47
  • 459

DBCP、C3P0,JNDI 连接池配置使用

  • 2012年05月18日 17:34
  • 1.76MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用 TFDConnection 的 pooled 连接池
举报原因:
原因补充:

(最多只允许输入30个字)