使用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;

  

 

posted @ 2014-11-27 16:35  TryHard  阅读(4120)  评论(0编辑  收藏  举报