zoukankan      html  css  js  c++  java
  • FireDAC的连接池

    FireDAC的连接池和一般理解的连接池有些区别。FireDAC的架构是分层设计的,平常我们用的都是高层的应用层。比如TFDConnection,具体连接什么,还要物理层的支持,就是那些TFDPhysXXXLink这些控件。所以只要一个FDConnection控件,就可以直接支持池,只要设置了池连接参数,开启池功能,就可以直接使用。(不是平常理解的建立一个FDConnection列表,然后外部管理这个列表来实现池,原来的TSQLConnection也是这样的,内部直接支持池。)。同时,用TFDManager来管理物理连接参数列表(TFDManager可以建立持久的(有唯一名字、保存在ini文件的),私有的(有唯一名字、只存在TFDManager中,不保存到ini文件),临时的(无名字,也不要TFDManager管理,直接在FDConnection控件设置,比如我们在设计界面打开FDConnection控件的连接设置界面,且不从以定义的列表选择连接名,而是直接设置各个连接参数,也就是直接保存在FDConnection.Params中)),只有持久的,私有的可以建立连接池,并且可以共享给其它代码使用(具体见帮助文档)。

    FireDAC supports 3 connection definition kinds:

    TypeDescriptionProsCons
    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. 

    这3种连接的建立DEMO,帮助文档也有示例。

    用FDManager保存的持久或私有连接参数,可以这样

    持久的(ini文件):

    [DataSnapCONNECTION]
    DriverName=DataSnap
    HostName=localhost
    port=211

    [MSSQLConnection]
    SchemaOverride=sa.dbo
    DriverName=MSSQL
    HostName=ServerName
    DataBase=Database Name
    User_Name=user
    Password=password
    BlobSize=-1
    ErrorResourceFile=
    LocaleCode=0000
    IsolationLevel=ReadCommitted
    OS Authentication=False
    Prepare SQL=False
    ConnectTimeout=60
    Mars_Connection=False

    [MySQLConnection]
    DriverName=MySQL
    HostName=ServerName
    Database=DBNAME
    User_Name=user
    Password=password
    ServerCharSet=
    BlobSize=-1
    ErrorResourceFile=
    LocaleCode=0000
    Compressed=False
    Encrypted=False
    ConnectTimeout=60

    [OracleConnection]
    DriverName=Oracle
    DataBase=Database Name
    User_Name=user
    Password=password
    RowsetSize=20
    BlobSize=-1
    ErrorResourceFile=
    LocaleCode=0000
    IsolationLevel=ReadCommitted
    OS Authentication=False
    Multiple Transaction=False
    Trim Char=False
    Decimal Separator=.

    [SQLITECONNECTION]
    DriverName=Sqlite
    Database=test.db

    以上摘录自delphi自带的dbxconnections.ini文件,一般保存在路径:C:UsersPublicDocumentsEmbarcaderoStudioFireDACFDConnectionDefs.ini.  

    私有的:

    var
      oParams: TStrings;
    begin
      oParams := TStringList.Create;
      oParams.Add('Database=ORA_920_APP');
      oParams.Add('User_Name=ADDemo');
      oParams.Add('Password=a');
      oParams.Add('Pooled=True');
      FDManager.AddConnectionDef('Oracle_Pooled', 'Ora', oParams);
      .....................
      FDConnection1.ConnectionDefName := 'Oracle_Pooled';
      FDConnection1.Connected := True;
    或者:

    with FDManager.ConnectionDefs.AddConnectionDef do
    begin
    Name := 'MSSQL_Connection';
    Params.Values['DriverID'] := 'MSSQL';
    Params.Values['Server'] := '(Local)';
    Params.Values['Database'] := 'Northwind';
    Params.Values['OSAuthent'] :='No';
    Params.Values['User_Name'] := 'sa';
    Params.Values['Password'] := '123';
    Params.PoolMaximumItems := 50; //系统默认50
    Params.Pooled := True;  //这个是关键
    end;

    ..........
    FDConnection.ConnectionDefName := 'MSSQL_Connection';

    FDConnection.Connected := True;

    当FDConnection.Connected := True;时,就会从连接池里获得一个物理连接,设置为False,物理连接就释放回连接池(注意释放回池里的FDConnection的连接状态保持为连接!!!)。

    要关闭和摧毁物理连接,用:

    FDManager.CloseConnectionDef('MSSQL_Connection');
    或者全部:
    FDManager.Close;

    其它的一些时间参数设置(摘自帮助文档):
    ParameterParameterExample
    POOL_CleanupTimeout The time (msecs) until FireDAC removes the connections that have not been used for longer than the POOL_ExpireTimeout time. The default value is 30000 msecs (30 secs). 3600000
    POOL_ExpireTimeout The time (msecs) after which the inactive connection may be deleted from the pool and destroyed. The default value is 90000 msecs (90 secs). 600000
    POOL_MaximumItems The maximum number of connections in the pool. When the application requires more connections, then an exception is raised. The default value is 50. 100 
    在一般单线程运用中,用一个TFDConnection来建立连接,只要打开池并且设置了FDManager连接参数,就可以了。
    在多程序运用中,每个线程还是要创建一个TFDConnection,然后各个线程可以共享FDManager,因为
    FDManager是单实例的。(我是这么理解的,多线程中,FDConnection不是池化的,但是物理连接是池化的)。

    DELPHI有2个Sample:
    C:UsersPublicDocumentsEmbarcaderoStudio19.0SamplesObject PascalDatabaseFireDACSamplesComp LayerTFDConnectionPooling
    C:UsersPublicDocumentsEmbarcaderoStudio19.0SamplesObject PascalDatabaseFireDACSamplesPhys LayerIFDPhysConnectionPooling

     

  • 相关阅读:
    JS 跨域问题。。
    LInq 中使用正则表达试
    CreateXMl
    DeleteXMl
    SameNameFile 比较两个文件夹是否同名
    UpdateXML
    AddXML
    AsDataView Dataview ,DataTable 跟linq的相互转化
    AttributeToElement
    WoreTime 计算单词出现的次数
  • 原文地址:https://www.cnblogs.com/jankerxp/p/10621851.html
Copyright © 2011-2022 走看看