⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uadoconnectionpool.pas

📁 数据库连接池。很重要很好的参考例子。全部源码
💻 PAS
字号:
////////////////////////////////////////////////////////////////////////////////
//
// The uADOConnectionPool unit contains ADOConnectionPool classes.
//
unit uADOConnectionPool;

interface

uses
  Classes, DB, uConnectionPool, ADODB;

type

  //////////////////////////////////////////////////////////////////////////////
  //
  // Summary:
  //   TADOPoolConnection represents a connection in a ADO connection pool
  //   (TADOConnectionPool).
  //
  // Description:
  //   Each TADOConnectionPool uses a TPoolConnections to maintain a
  //   collection of TADOPoolConnection objects. Each TADOPoolConnection object
  //   represents the single database connection (TADOConnection) in the pool.
  //
  TADOPoolConnection = class(TPoolConnection)
  protected
    procedure Lock; override;
    procedure Unlock; override;
    function CreateConnection: TCustomConnection; override;
  end;

  //////////////////////////////////////////////////////////////////////////////
  //
  // Summary:
  //   TADOConnectionPool is a connection pool realisation for ADO.
  //
  // SeeAlso:
  //  TCustomConnectionPool, TADOConnection
  //
  TADOConnectionPool = class(TCustomConnectionPool)
  private
    FCommandTimeout: Integer;
    FConnectionTimeout: Integer;
    FDefaultDatabase: String;
    FConnectionString: WideString;
    FMode: TConnectMode;
    FConnectOptions: TConnectOption;
    FCursorLocation: TCursorLocation;
    FIsolationLevel: TIsolationLevel;
    FAttributes: TXactAttributes;
  protected
    function GetPoolItemClass: TPoolConnectionClass; override;
  public
    constructor Create(aOwner: TComponent); override;
    procedure Assign(Source: TPersistent); override;
    procedure AssignTo(Dest: TPersistent); override;
  published

    //
    // Summary:
    //   Specifies automated transaction behavior.
    //
    // Description:
    //   Set Attributes to specify whether a connection object performs
    //   retaining commits or retaining aborts. The two behaviors are
    //   independent of each other.
    //
    //   Attributes can contain one of the two TXactAttribute values
    //   (xaCommitRetaining and xaAbortRetaining), both values, or neither
    //   value.
    //
    // SeeAlso:
    //   TADOConnection.Attributes
    //
    property Attributes: TXactAttributes read FAttributes write FAttributes;

    //
    // Summary:
    //  Specifies the connection information for the data store.
    //
    // Description:
    //   Set ConnectionString to specify the information needed to connect the
    //   ADO connection component to the data store. The value used for
    //   ConnectionString consists of one or more arguments ADO uses to
    //   establish the connection. Specify multiple arguments as a list with
    //   individual arguments separated by semicolons.
    //
    // SeeAlso:
    //   TADOConnection.ConnectionString
    //
    property ConnectionString: WideString read FConnectionString write FConnectionString;

    //
    // Summary:
    //  Specifies amount of time to attempt execution of a command.
    //
    // Description:
    //  Use CommandTimeout to specify the amount of time, in seconds, that that
    //  can expire before an attempt to execute a command is considered
    //  unsuccessful. The default value is 30 seconds.
    //
    // SeeAlso:
    //   TADOConnection.CommandTimeout
    //
    property CommandTimeout: Integer read FCommandTimeout write FCommandTimeout default 30;

    //
    // Summary:
    //   Specifies amount of time to attempt a connection.
    //
    // Description:
    //   Use ConnectionTimeout to specify the amount of time, in seconds, that
    //   can expire before an attempt to make a connection is considered
    //   unsuccessful. The default value is 15 seconds.
    //
    //   If a connection is successfully made prior to the expiration of the
    //   seconds specified or the Cancel method is called, ConnectionTimeout has
    //   no effect. If the specified time expires and a connection has not been
    //   successfully made, the attempt is terminated and an exception is
    //   raised.
    //
    // SeeAlso:
    //   TADOConnection.ConnectionTimeout
    //
    property ConnectionTimeout: Integer read FConnectionTimeout write FConnectionTimeout default 15;

    //
    // Summary:
    //   Specifies whether a connection is synchronous or asynchronous.
    //
    // Description:
    //   Set ConnectOptions to specify whether the connection established by the
    //   TADOConnection is synchronous or asynchronous. The default value for
    //   ConnectOptions is coConnectUnspecified.
    //
    //   An application will typically use the default of a synchronous
    //   connection (coConnectUnspecified). Typically an asynchronous is only
    //   really needed to compensate for a slow server.
    //
    // SeeAlso:
    //   TADOConnection.ConnectOptions
    //
    property ConnectOptions: TConnectOption read FConnectOptions write FConnectOptions default coConnectUnspecified;

    //
    // Summary:
    //   Specifies whether cursors for the connection is client-side or
    //   server-side.
    //
    // Description:
    //   Use CursorLocation to indicate whether the cursors that use the
    //   connection object to connect to the ADO datastore use a client-side or
    //   server-side cursor library. CursorLocation only affects connections
    //   opened after the property is set. The default value for
    //   CursorLocation is clUseClient.
    //
    // SeeAlso:
    //   TADOConnection.CursorLocation
    //
    property CursorLocation: TCursorLocation read FCursorLocation write FCursorLocation default clUseClient;

    //
    // Summary:
    //   Indicates the database the ADO connection uses by default.
    //
    // Description:
    //   Set DefaultDatabase to indicate the database the ADO connection objects
    //   uses if the connection to the database specified in ConnectionString is
    //   unavailable, the connection cannot be made, or the database is not
    //   specified in the connection string.
    //
    // SeeAlso:
    //   TADOConnection.DefaultDatabase
    //
    property DefaultDatabase: String read FDefaultDatabase write FDefaultDatabase;

    //
    // Summary:
    //   Specifies the transaction isolation level for transactions.
    //
    // Description:
    //   Use IsolationLevel to specify the transaction isolation level for a
    //   connection. The transaction isolation level determines how a
    //   transaction interacts with other simultaneous transactions when they
    //   work with the same tables, and how much a transaction sees of the work
    //   performed by other transactions. The default value for IsolationLevel
    //   is ilCursorStability.
    //
    // SeeAlso:
    //   TADOConnection.IsolationLevel
    //
    property IsolationLevel: TIsolationLevel read FIsolationLevel write FIsolationLevel default ilCursorStability;

    //
    // Summary:
    //   Indicate the persimissions available to a connection.
    //
    // Description:
    //   Read Mode to determine the permissions available to the connection
    //   after the connection has been activated. The permissions expressed in
    //   Mode govern the types of operations (such as reading and writing) can
    //   be performed through the connection. They also control how these
    //   operations are affected by and affect other concurrent users.
    //
    // SeeAlso:
    //   TADOConnection.Mode
    //
    property Mode: TConnectMode read FMode write FMode default cmUnknown;

    property MaxConnections;
    property OnLockConnection;
    property OnUnlockConnection;
    property OnCreateConnection;
    property OnLockFail;
    property OnFreeConnection;    
  end;

implementation

uses
  SysUtils;

{ TADOConnectionPoolItem }

{- protected ------------------------------------------------------------------}
function TADOPoolConnection.CreateConnection: TCustomConnection;
begin
  Result:= TADOConnection.Create(nil);
  with Result as TADOConnection do
  begin
    LoginPrompt:= false;
    ConnectionString:= TADOConnectionPool(TPoolConnections(Collection).Owner).ConnectionString;
    CommandTimeout:= TADOConnectionPool(TPoolConnections(Collection).Owner).FCommandTimeout;
    ConnectionTimeout:= TADOConnectionPool(TPoolConnections(Collection).Owner).ConnectionTimeout;
    ConnectOptions:= TADOConnectionPool(TPoolConnections(Collection).Owner).ConnectOptions;
    CursorLocation:= TADOConnectionPool(TPoolConnections(Collection).Owner).CursorLocation;
    DefaultDatabase:= TADOConnectionPool(TPoolConnections(Collection).Owner).DefaultDatabase;
    IsolationLevel:= TADOConnectionPool(TPoolConnections(Collection).Owner).IsolationLevel;
    Mode:= TADOConnectionPool(TPoolConnections(Collection).Owner).Mode;
  end;
end;

procedure TADOPoolConnection.Lock;
begin
  inherited;
  (Connection as TADOConnection).BeginTrans;
end;

procedure TADOPoolConnection.Unlock;
begin
  inherited;
  if (Connection as TADOConnection).InTransaction then
  try
    (Connection as TADOConnection).CommitTrans;
  except
    (Connection as TADOConnection).RollbackTrans;
  end;
end;

{ TADOConnectionPool }

{- protected ------------------------------------------------------------------}
function TADOConnectionPool.GetPoolItemClass: TPoolConnectionClass;
begin
  Result:= TADOPoolConnection;
end;

{- public ---------------------------------------------------------------------}
constructor TADOConnectionPool.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);
  FConnectionString:= '';
  FCommandTimeout:= 30;
  FConnectionTimeout:= 15;
  FConnectOptions:= coConnectUnspecified;
  FCursorLocation:= clUseClient;
  FDefaultDatabase:= '';
  FIsolationLevel:= ilCursorStability;
  FMode:= cmUnknown;
end;

procedure TADOConnectionPool.Assign(Source: TPersistent);
begin
  if Source is TADOConnection then
  begin
    ConnectionString:= TADOConnection(Source).ConnectionString;
    CommandTimeout:= TADOConnection(Source).CommandTimeout;
    ConnectionTimeout:= TADOConnection(Source).ConnectionTimeout;
    ConnectOptions:= TADOConnection(Source).ConnectOptions;
    CursorLocation:= TADOConnection(Source).CursorLocation;
    DefaultDatabase:= TADOConnection(Source).DefaultDatabase;
    IsolationLevel:= TADOConnection(Source).IsolationLevel;
    Mode:= TADOConnection(Source).Mode;
  end
  else
    inherited;
end;

procedure TADOConnectionPool.AssignTo(Dest: TPersistent);
begin
  if Dest is TADOConnectionPool then
  begin
    TCustomConnectionPool(Dest).MaxConnections:= MaxConnections;
    TADOConnectionPool(Dest).ConnectionString:= ConnectionString;
    TADOConnectionPool(Dest).CommandTimeout:= CommandTimeout;
    TADOConnectionPool(Dest).ConnectionTimeout:= ConnectionTimeout;
    TADOConnectionPool(Dest).ConnectOptions:= ConnectOptions;
    TADOConnectionPool(Dest).CursorLocation:= CursorLocation;
    TADOConnectionPool(Dest).DefaultDatabase:= DefaultDatabase;
    TADOConnectionPool(Dest).IsolationLevel:= IsolationLevel;
    TADOConnectionPool(Dest).Mode:= Mode;
  end
  else
  if Dest is TADOConnection then
  begin
    TADOConnection(Dest).ConnectionString:= ConnectionString;
    TADOConnection(Dest).CommandTimeout:= CommandTimeout;
    TADOConnection(Dest).ConnectionTimeout:= ConnectionTimeout;
    TADOConnection(Dest).ConnectOptions:= ConnectOptions;
    TADOConnection(Dest).CursorLocation:= CursorLocation;
    TADOConnection(Dest).DefaultDatabase:= DefaultDatabase;
    TADOConnection(Dest).IsolationLevel:= IsolationLevel;
    TADOConnection(Dest).Mode:= Mode;
  end
  else
    inherited;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -