您的位置:首页 > 其它

FIREDAC FDConnection 连接池

2015-08-21 19:09 330 查看

一、FDConnection 连接池

http://docs.embarcadero.com/products/rad_studio/firedac/frames.html?frmname=topic&frmfile=Defining_Connection.html
http://docwiki.embarcadero.com/RADStudio/XE8/en/Defining_Connection_%28FireDAC%29
FDManager()->ConnectionDefs->ConnectionDefByName("")

FDManager.ConnectionDefs.ConnectionDefByName(cbDB.Text).Params.Pooled := True

oConn.ConnectionDefName;

FDConnection1->Params->Pooled;

$(FDHOME)\FDConnectionDefs.ini

D:\Users\Public\Documents\Embarcadero\Studio\FireDAC\FDConnectionDefs.ini

D:\Users\Public\Documents\Embarcadero\Studio\16.0\Samples\Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDConnection\Pooling

从这个官方例子看出使用连接池比不使用连接池的效率提高4倍。
http://www.cnblogs.com/zhenfei/p/4105515.html

1)FDConnectionDefs.ini文件

[MSSQL_Demo]
DriverID=MSSQL
Server=127.0.0.1
Database=Northwind
User_Name=sa
Password=
MetaDefSchema=dbo
MetaDefCatalog=Northwind
ExtendedMetadata=True

以上代码存为FDConnectionDefs.ini文件放到当前exe目录下,程序会自己找到。小节名称与ConnectionDefByName参数一致。

this->FDManager1->ConnectionDefFileName = "FDConnectionDefs.ini";
this->FDManager1->ConnectionDefs->ConnectionDefByName("MSSQL_Demo")->Params->Pooled = true;

考虑到安全性,密码需要加密,所以建议用方法2或3。

2)代码动态创建IFDStanConnectionDef

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;

3)连接参数

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;

c++  FDManager()->AddConnectionDef("", "", para);
其他

FDManager.ConnectionDefFileName := ExtractFilePath(Application.ExeName) + 'myconndef.ini';
FDManager.ConnectionDefFileAutoLoad := True;
oConn := TFDConnection.Create(nil);
oConn.ConnectionDefName := 'myconn';
oConn.Connected := True;


Create Connection def

void __fastcall TForm35::Button5Click(TObject *Sender)
{
TStrings *para;
para = new TStringList();
para->Add("Server=192.168.1.1");
para->Add("Database=db");
para->Add("User_Name=sa");
para->Add("Password=123");

FDManager()->AddConnectionDef("sqlPTT", "MSSQL", para);

FDConnection1->ConnectionDefName = "sqlPTT";
Label1->Caption = this->FDConnection1->ResultConnectionDef->BuildString();
delete para;

int startTime = GetTickCount();
for (int i = 0; i < 10; i++)
{
this->Button1->Click();
}
Caption = (GetTickCount() - startTime) / 1000.0;
}


use pool

void __fastcall TForm35::Button1Click(TObject *Sender)
{
TFDConnection *con;
con = new TFDConnection(NULL);
con->ConnectionDefName = FDConnection1->ConnectionDefName;

TFDQuery *qry;
qry = new TFDQuery(NULL);
qry->Connection = con;
qry->SQL->Text = "select count(*) from tt";
qry->Open();
con->Close();

delete con;
delete qry;

}


连接池比普通连接块4倍!

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

mysql
Database=testdata;User_Name=root;Password=root;Server=127.0.0.1;DriverID=MySQL;CharacterSet=utf8

sqlite:
Database=D:\msdb.db;DriverID=SQLite

执行十条sql语句性能比较。
1、一个连接打开,不关闭,耗时0.184ms
2、使用连接池,耗时0.265ms
3、不使用连接池,0.71ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: