Database Connection Pool Data Connection Framework

Source: Internet
Author: User

The Data Connection Framework of the Database Connection Pool provides the Connection pooling mechanism to manage different ADO.net connections. And create a data connection pool through the PoolObjectFactory of the pool object factory. (Refer to: create an Object factory --- Singleton Object, SingleCall Object, Pool Object)

 

The following figure shows the enterprise application components of the Connection Pool:

 

The class diagram is as follows:

 

Main file code:

 

/// <Summary>
/// Connection pool Context
/// </Summary>
Public sealed class ConnectionPoolContext: IDisposable
{
/// <Summary>
/// Pool object Factory
/// </Summary>
Private PoolObjectFactory _ factory;
/// <Summary>
/// Default connection character
/// </Summary>
Private string _ defaultonstring;
/// <Summary>
/// Factory name
/// </Summary>
Private string _ factoryName;
/// <Summary>
/// Type
/// </Summary>
Private Type _ factoryType;
/// <Summary>
/// SQL status warehouse
/// </Summary>
Private SqlStatementRepository _ statementRepository;
/// <Summary>
/// Connection pool
/// </Summary>
Private static Dictionary <string, ConnectionPoolContext> _ instances = new Dictionary <string, ConnectionPoolContext> ();
/// <Summary>
/// Default connection character attribute
/// </Summary>
Public string defaultonstring
{
Get
{
Return _ defaconnectionstring;
}
Set
{
_ Defaconnectionstring = value;
}
}
/// <Summary>
/// Factory name
/// </Summary>
Public string FactoryName
{
Get
{
Return _ factoryName;
}
Set
{
_ FactoryName = value;
}
}
/// <Summary>
/// SQL status repository attributes
/// </Summary>
Public SqlStatementRepository StatementRepository
{
Get
{
If (_ statementRepository = null)
_ StatementRepository = new SqlStatementRepository ();
Return _ statementRepository;
}
}
/// <Summary>
/// Object factory Mode
/// </Summary>
Internal PoolObjectFactory
{
Get
{
Return _ factory;
}
Set
{
_ Factory = value;
}
}
/// <Summary>
/// Instantiate a connection pool
/// </Summary>
/// <Param name = "id"> </param>
/// <Returns> </returns>
Public static ConnectionPoolContext Instance (string id)
{
Return _ instances [id];
}
/// <Summary>
/// Add a connection to the pool
/// </Summary>
/// <Param name = "id"> id </param>
/// <Param name = "factoryName"> factory name </param>
/// <Param name = "connectionString"> connection string </param>
/// <Param name = "max"> maximum number </param>
/// <Param name = "limit"> limit </param>
Public static void AddPool (string id, string factoryName, string connectionString, int max, bool limit)
{
ConnectionPoolContext p = new ConnectionPoolContext ();
P. _ defaconnectionstring = connectionString;
P. _ factoryName = factoryName;
P. Factory = new PoolObjectFactory (max, limit, new ArrayList ());
_ Instances. Add (id, p );
}
/// <Summary>
/// Get a connection
/// </Summary>
/// <Returns> database connection </returns>
Public DbConnection AcquireConnection ()
{
If (_ factoryType = null)
{
DbProviderFactory dbFactory = DbProviderFactories. GetFactory (_ factoryName );
DbConnection conn = dbFactory. CreateConnection ();
_ FactoryType = conn. GetType ();
}
DbConnection connection = (DbConnection) Factory. AcquireObject (_ factoryType );
If (connection. ConnectionString = string. Empty)
Connection. ConnectionString = _ defaultConnectionString;
Return connection;
}
/// <Summary>
/// Release a connection
/// </Summary>
/// <Param name = "conn"> database connection </param>
Public void ReleaseConnection (DbConnection conn)
{
Factory. ReleaseObject (conn );
Conn. Close ();
}

# Region IDisposable Members
/// <Summary>
/// Release the connection
/// </Summary>
Public void Dispose ()
{
If (_ factory! = Null)
(IDisposable) _ factory). Dispose ();
}

# Endregion
/// <Summary>
/// Private constructor
/// </Summary>
Private ConnectionPoolContext ()
{
}
}

URL: http://putishu.cnblogs.com/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.