使用Unidac内置连接池

第一步:

   放一个TUniconnection并设置相关属性

之后直接使用TUniconnection对象即可

 

跟踪unidac源码uni单元1540行中可以看到

Connect方法调用CreateIConnection

 

procedure TUniConnection.CreateIConnection;
var
  Connection: TCRConnection;
  ConnectionParameters: TCRConnectionParameters;

  procedure SetSpecificObjectProps(SetAllProps: boolean);
  begin
    FProvider.SetObjectProps(Connection, FSpecificOptions.Values, SetAllProps);
    FSpecificOptions.IsModified := False;
  end;

begin
  CheckProvider;

  Connection := FIConnection;

  if Connection = nil then begin
    if Pooling and FProvider.IsPoolingSupported then begin
      ConnectionParameters := FProvider.GetConnectionParametersClass.Create;
      try
        ConnectionParameters.MinPoolSize := PoolingOptions.MinPoolSize;
        ConnectionParameters.MaxPoolSize := PoolingOptions.MaxPoolSize;
        ConnectionParameters.ConnectionLifeTime := PoolingOptions.ConnectionLifetime;
        ConnectionParameters.Validate := PoolingOptions.Validate;
        ConnectionParameters.Username := Username;
        ConnectionParameters.Password := Password;
        ConnectionParameters.Server := Server;
        ConnectionParameters.IOHandler := FIOHandler;
        ConnectionParameters.OnError := DoError;
        if FProvider.IsDatabaseSupported then //upd1
          ConnectionParameters.SetProp(prDatabase, FDatabase);
        if FProvider.IsPortSupported then
          ConnectionParameters.SetProp(prPort, Port);

        FProvider.SetObjectProps(ConnectionParameters, SpecificOptions, True);

        Connection := FProvider.GetConnectionPoolingManagerClass.GetConnection(
          ConnectionParameters, TUniSQLMonitor);
      finally
        ConnectionParameters.Free;
      end;
    end
    else begin
      Connection := GetIConnectionClass.Create;
      Connection.IOHandler := FIOHandler;
      if FProvider.IsDatabaseSupported then
        Connection.SetProp(prDatabase, FDatabase);
      if FProvider.IsPortSupported then
        Connection.SetProp(prPort, Port);
    end;

    Connection.SetProp(prDisconnectedMode, Options.DisconnectedMode);
    Connection.SetProp(prEnableBCD, Options.EnableBCD);
  {$IFDEF VER6P}
  {$IFNDEF FPC}
    Connection.SetProp(prEnableFMTBCD, Options.EnableFMTBCD);
  {$ENDIF}
  {$ENDIF}
    Connection.SetProp(prDefaultSortType, Variant(Options.DefaultSortType));
    // if connection is just created we need to set all options
    SetSpecificObjectProps(True);

    SetIConnection(Connection);
  end;

  if FSpecificOptions.IsModified then
    SetSpecificObjectProps(False);
end;

  

 

转载于:https://www.cnblogs.com/pengshaomin/p/4126662.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页