1. For the ASP. NET database connection pool, you can use a group of name-value pairs to configure the connection pool as a link string. For example, you can configure whether the pool is valid (which is valid by default). The maximum and minimum capacity of the pool is used for the time when the queued requests that open the link are blocked. The following example string configures the maximum and minimum capacity of the pool.
"Server = (local); Integrated Security = sspi; database = northwind;
Max pool size = 75; min pool size = 5"
Summary of ASP. NET database connection pool
Application allowed in connection pool Program Obtain a connection from the connection pool and use the connection, without re-establishing a connection for each connection request. Once a new connection is created and placed in the connection pool, the application can reuse the connection without the entire database connection creation process.
When an application requests a connection, the connection pool allocates a connection for the application instead of creating a new connection. After the application uses the connection, the connection is returned to the connection pool instead of being released directly.
2,
The connection pool reduces the number of times new connections need to be opened. The pool Process maintains the ownership of the physical connection. Manage connections by retaining a set of active connections for each given connection configuration. As long as the user calls open on the connection, the pool process checks whether there are available connections in the pool. If a pool connection is available, the connection is returned to the caller instead of opening a new connection. When an application calls close on the connection, the pool process returns the connection to the active connection pool instead of closing the connection. After the connection is returned to the pool, it can be reused in the next open call.
You can establish a pool connection only when the same connection is configured. Ado. Net retains multiple pools at the same time, and each pool is configured with one. Connections are divided into multiple pools by connection strings and Windows Identifiers (when using integrated security.
Pool connections can greatly improve application performance and scalability. By default, the connection pool is enabled in ADO. net. Unless explicitly disabled, the pool process will optimize the connection when the connection is opened or closed in the application. You can also provide several connection string modifiers to control connection pool behavior.
Pool creation and allocation
When the connection is opened for the first time Algorithm Create a connection pool. This algorithm associates the pool with the connection string in the connection. Each connection pool is associated with different connection strings. When a new connection is opened, if the connection string does not match the existing pool, a new pool is created. Create a pool connection by process, by application domain, by connection string, and by windows when using integrated security.
In the following C # example, three new sqlconnection objects are created, but only two connection pools are required for management. Note that the first and second connection strings are different based on the value assigned for the initial catalog.
CopyCode The Code is as follows: Using (sqlconnection connection = new sqlconnection (
"Integrated Security = sspi; initial catalog = northwind "))
{
Connection. open ();
// Pool a is created.
}
Using (sqlconnection connection = new sqlconnection (
"Integrated Security = sspi; initial catalog = pubs "))
{
Connection. open ();
// Pool B is created because the connection strings differ.
}
Using (sqlconnection connection = new sqlconnection (
"Integrated Security = sspi; initial catalog = northwind "))
{
Connection. open ();
// The connection string matches pool.
}
If minpoolsize is not specified in the connection string or is specified as zero, the connection in the pool will be closed after a period of inactivity. However, if the specified minpoolsize is greater than zero, the connection pool will not be damaged until the appdomain is detached and the process ends. To maintain a non-active or empty pool, only the minimum system overhead is required.
Add a connection
the connection pool is created for each unique connection string. After a pool is created, multiple connection objects are created and added to the pool to meet the minimum pool size requirements. The connection is added to the pool as needed, but cannot exceed the specified maximum pool size (default value: 100 ). The connection is released back to the pool when it is closed or disconnected.
If an available connection exists when you request a sqlconnection object, the object is obtained from the pool. The connection must be available, not used, have a matched transaction context or not associated with any transaction context, and have a valid link with the server.
the connection pool process re-allocates connections when the connection is released back to the pool to meet these connection requests. If the maximum pool size is reached and no available connections exist, the request will be queued. Then, the pool process tries to re-establish any connection until the timeout time is reached (the default value is 15 seconds ). If the pool process cannot meet the request before the connection times out, an exception is thrown.
remove a connection
the connection pool process periodically scans the connection pool to find unused connections that are not closed through close or dispose and reestablishes the connection. If the application does not explicitly close or disconnect it, it may take a long time for the connection pool process to re-establish the connection. Therefore, it is best to ensure that close and dispose are explicitly called in the connection.
If the connection is idle for a long time or the pool process detects that the connection with the server is disconnected, the connection pool process removes the connection from the pool. Note that the disconnected connection can be detected only after you try to communicate with the server. If a connection is no longer connected to the server, it is marked as invalid. Invalid connections are removed from the connection pool only after they are closed or re-established.
If a connection exists with a deleted server, even if the connection pool Administrator does not detect a disconnected connection and marks it as invalid, it is still possible to retrieve the connection from the pool. This is because the system overhead of checking whether the connection is still valid will cause another round-trip with the server, thus offsetting the advantages of the pool process. In this case, the first attempt to use the connection will detect whether the connection has been disconnected and cause an exception.
Advantages of ASP. NET database connection pool
The main advantage of using a connection pool is performance. The time required to create a new database connection mainly depends on the speed of the network and the distance between the application and the database server (network). This process is usually a very time-consuming process. After the database connection pool is used, database connection requests can be directly met through the connection pool without re-connecting and authenticating the request to the database server. This saves time.
Disadvantages of ASP. NET database connection pool
There may be multiple unused connections in the database connection pool that have been connected to the database (this means a waste of resources ).
Tips
1. Create a connection pool only when you need a database connection, instead of creating a connection pool in advance. Once you use the connection, immediately close it. Do not wait until the Garbage Collector processes it.
2. Make sure that all user-defined transactions are closed before closing the database connection.
3. Do not close all connections in the database. at least ensure that one connection in the connection pool is available. If the memory and other resources are the first thing you must consider, you can close all connections and create a connection pool when the next request arrives.