📄 uconnectionpool.pas
字号:
////////////////////////////////////////////////////////////////////////////////
//
// The uConnectionPool unit contains base classes of Connection Pool.
//
unit uConnectionPool;
interface
uses
Classes, DB, SyncObjs, SysUtils;
type
TPoolConnectionClass = class of TPoolConnection;
TCustomConnectionPool = class;
//////////////////////////////////////////////////////////////////////////////
//
// Summary:
// TPoolConnection represents a connection in a connection pool
// (TCustomConnectionPool).
//
// Description:
// Each TCustomConnectionPool uses a TPoolConnections to maintain a
// collection of TPoolConnection objects. Each TPoolConnection object
// represents the single database connection in the pool.
//
TPoolConnection = class(TCollectionItem)
private
FBusy: Boolean;
FConnection: TCustomConnection;
protected
//
// Summary:
// Locks the connection in the connection pool
//
// Description:
// Lock mark connection as "locked" and opens connection if it closed.
//
// SeeAlso:
// Busy, TCustomConnectionPool.GetConnection
//
procedure Lock; virtual;
//
// Summary:
// Unlocks the connection in the connection pool
//
// Description:
// Unlock mark connection as "unlocked". Unlocked connection can be
// retreived by calling TCustomConnectionPool.GetConnection
//
// SeeAlso:
// Busy, TCustomConnectionPool.FreeConnection
//
procedure Unlock; virtual;
//
// Summary:
// Connected checks database connection
//
// Result:
// True - if connection connected to database
// False - otherwise
//
// SeeAlso:
// Connection
//
function Connected: Boolean; virtual;
//
// Summary:
// CreateConnection creates connection element
//
// Result:
// Newly created TCustomConnection descendant
//
// SeeAlso:
// Connection
//
function CreateConnection: TCustomConnection; virtual; abstract;
public
//
// Summary:
// Indicates that the connection has been locked.
//
// Description:
// Busy is a Boolean property that indicates when the connection has been
// locked.
//
// SeeAlso:
// Lock, Unlock
//
property Busy: Boolean read FBusy;
//
// Summary:
// Contains TCustomConnection descendants represented by this connection
// pool item.
//
// SeeAlso:
// TCustomConnectionPool.GetConnection,
// TCustomConnectionPool.FreeConnection
//
property Connection: TCustomConnection read FConnection;
//
// Summary:
// Creates and initializes a TPoolConnection instance.
//
// Description:
// The Create method takes as a parameter the name of a TCollection
// instance. Create is called by TPoolConnection抯 Add method.
//
// SeeAlso:
// TPoolConnections.Add
//
constructor Create(aCollection: TCollection); override;
//
// Summary:
// Destroys the TPoolConnection instance and frees its memory.
//
// Description:
// Destroy is called indirectly by TCollection抯 Clear method.
//
// SeeAlso:
// TCollection.Clear
//
destructor Destroy; override;
end;
//////////////////////////////////////////////////////////////////////////////
//
// Summary:
// TPoolConnections is a container for TPoolConnection objects.
//
// Description:
// Each TPoolConnections holds a collection of TPoolConnection objects in a
// connection pool (TCustomConnectionPool). TPoolConnections maintains an
// index of the connection in its Items array. The Count property contains
// the number of connections in the collection.
//
// SeeAlso:
// TCustomConnectionPool, TPoolConnection
//
TPoolConnections = class(TOwnedCollection)
private
function GetItem(aIndex: Integer): TPoolConnection;
procedure SetItem(aIndex: Integer; const Value: TPoolConnection);
public
//
// Summary:
// Provides indexed access to the items in the collection.
//
// Description:
// Use Items to access individual items in the collection. The value of
// the Index parameter corresponds to the Index property of
// TPoolConnection. It represents the position of the item in the
// collection.
//
property Items[aIndex: LongInt]: TPoolConnection read GetItem write SetItem; default;
//
// Summary:
// Creates a new TPoolConnection instance and adds it to the Items array.
//
// Description:
// Call Add to create an item in the collection. The new item is placed at
// the end of the Items array.
//
// Result:
// Add returns the new collection item.
//
function Add: TPoolConnection;
{$IFNDEF VER140}
//
// Summary:
// Returns the Owner of the collection.
//
// Description:
// Call Owner to obtain a reference to the object that owns this
// collection. Typically, the owner uses the collection to implement one
// of its properties.
//
function Owner: TPersistent;
{$ENDIF}
end;
TExceptionEvent = procedure (Sender: TObject; E: Exception) of object;
//////////////////////////////////////////////////////////////////////////////
//
// Summary:
// TCustomConnection is the base class for components that represents a pool
// of connections to one database.
//
// Description:
// Use TCustomConnection as a base class for components that represent a
// poolof connections to one database. Do not create instances of
// TCustomConnectionPool. To add a component that represents the connection
// pool, use a TCustomConnectionPool descendant such as TBDEConnectioPool,
// TADOConnectionPool, TDBXConnectionPool.
//
// SeeAlso:
// TBDEConnectioPool, TADOConnectionPool, TDBXConnectionPool.
//
TCustomConnectionPool = class(TComponent)
private
FCS: TCriticalSection;
FConnections: TPoolConnections;
FMaxConnections: LongInt;
FOnLockConnection: TNotifyEvent;
FOnLockFail: TExceptionEvent;
FOnUnLockConnection: TNotifyEvent;
FOnCreateConnection: TNotifyEvent;
FOnFreeConnection: TNotifyEvent;
function GetUnusedConnections: LongInt;
function GetTotalConnections: LongInt;
protected
//
// Summary:
// Returns class of items into pool collection.
//
// Description:
// TCustomConnectionPool descendants overrides GetPoolItemClass for
// declaring class of items in the pool collection.
//
// Result:
// Class of items into pool collection.
//
function GetPoolItemClass: TPoolConnectionClass; virtual; abstract;
//
// Summary:
// Generates an OnLock event.
//
// Description:
// DoLock is called automatically when connection locked. Override this
// method to provide additional processing other than calling the OnLock
// event handler.
//
// SeeAlso:
// OnLock, TPoolConnection.Lock
//
procedure DoLock; virtual;
//
// Summary:
// Generates an OnLockFail event.
//
// Description:
// DoLockFail is called automatically when connection locking
// failed (Connection limit exceeded or any other error raised).
// Override this method to provide additional processing other than
// calling the OnLockFail event handler.
//
// SeeAlso:
// OnLockConnection, OnLockFail
//
procedure DoLockFail(E: Exception); virtual;
//
// Summary:
// Generates an OnUnlock event.
//
// Description:
// DoUnlock is called automatically when connection unlocked. Override
// this method to provide additional processing other than calling the
// OnUnlock event handler.
//
// SeeAlso:
// OnUnlock, TPoolConnection.Unlock
//
procedure DoUnlock; virtual;
//
// Summary:
// Generates an OnCreateConnection event.
//
// Description:
// DoCreateConnection is called automatically when new connection
// allocated. Override this method to provide additional processing other
// than calling the OnCreateConnection event handler.
//
// SeeAlso:
// OnCreateConnection
//
procedure DoCreateConnection; virtual;
//
// Summary:
// Generates an OnFreeConnection event.
//
// Description:
// OnFreeConnection is called automatically when connection destroyed
// Override this method to provide additional processing other than
// calling the OnCreateConnection event handler.
//
// SeeAlso:
// OnFreeConnection
//
procedure DoFreeConnection; virtual;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
// Summary:
// Copies the contents of another ConnectionPool or Connection.
//
// Description:
// Call Assign to copy the properties of Connection Pool from another
// ConnectionPool or Connection. The standard form of a call to Assign is
// <CODE>
// Destination.Assign(Source);
// </CODE>
procedure AssignTo(Dest: TPersistent); override;
//
// Summary:
// MaxConnections reprsents the max. number of connections in the pool.
//
// Description
// MaxConnections property is used to set the number of concurrent
// connections the connection pool will make to the database.
// If MaxConnections = -1, then number of connections is unlimited.
//
// SeeAlso:
// GetConnection
//
property MaxConnections: LongInt read FMaxConnections write FMaxConnections default -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -