This example illustrates how to create a connection pool to the data source. This operation is required to deploy high-performance applications. In
In this example, the pool is created in the connection string and automatically managed by sqlconnection.
In this example, specify the pool feature in the connection string when constructing sqlconnection, as shown in the following code:
In this example. Note: The pool processing is implicit. Unless disabled, the pool processing is automatically performed. Therefore,
"True" is the default pool keyword (pooling = true ).
String connstring;
Connstring =
"Server = (local) // vsdotnet; trusted_connection = yes; database = northwind;" + "Connection Reset = false;" + "connection lifetime = 5; "+" Min pool size = 1; "+" Max pool size = 50 ";
Sqlconnection myconnection1 = new sqlconnection (connstring); sqlconnection myconnection2 = new sqlconnection (connstring); sqlconnection myconnection3 = new sqlconnection (connstring );
Now we have several connection codes in the pool. First, open two connections and return the two to the pool
Connection. Then, open the three connections from the pool and return all the three connections to the pool.
// Open two connections. One is from the pool (see Min pool size), the other is created.
Console. writeline ("open two connections."); myconnection1.open (); myconnection2.open ();
// Now there are two connections in the pool that matches the connection string.
// Return the both connections to the pool.
Console. writeline ("return both of the connections to the pool."); myconnection1.close (); myconnection2.close ();
// Get a connection out of the pool.
Console. writeline ("open a connection from the pool."); myconnection1.open ();
// Get a second connection out of the pool.
Console. writeline ("open a second connection from the pool."); myconnection2.open ();
// Open a third connection.
Console. writeline ("open a third connection .");
When the connection is opened, a precise matching algorithm is used to create a connection pool.
. Each connection pool is associated with a different connection string. When a new connection is opened, as shown in figure
If the connection string does not match the existing pool accurately, a new pool is created.
In the following example, three new sqlconnection objects are created, but you only need to use two connection pools to manage
Manage these objects. Note that the difference between the first and second connection strings is that
Configuration value.
Sqlconnection conn = new sqlconnection (); Conn. connectionstring = "Integrated Security = sspi; initial catalog = northwind"; Conn. open (); // pool a is created.
Sqlconnection conn = new sqlconnection (); Conn. connectionstring = "Integrated Security = sspi; initial catalog = pubs"; Conn. open (); // pool B is created because the connection strings differ.
Sqlconnection conn = new sqlconnection (); Conn. connectionstring = "Integrated Security = sspi; initial catalog = northwind"; Conn. open (); // The connection string matches pool.
Once the connection pool is created, it will not be destroyed until the active process is terminated. Maintenance of inactive or empty pools only requires the least
System overhead.
Add connection
The connection pool is created for each unique connection string. After a pool is created, multiple connection objects are created and
It is added to the pool to meet the minimum pool size requirements. The connection will be added to the pool as needed until it reaches the maximum
Large pool size.
When a sqlconnection object is requested, if a connection is available, the object is obtained from the pool. To
Becomes an available connection. The connection must not be used currently. It has a matched transaction context or is not related to any transaction.
Associated with the server and has a valid link to the server.
If the maximum pool size is reached and no available connections exist, the request will be queued. When the connection is released back to the pool
The object pool manager will re-allocate connections to meet these requests. If you exceed
An error occurs when the time limit expires (determined by the connect timeout connection string attribute.
Warning you must disable connection after each connection operation. This can use the connection object's
Close or dispose method. Connections that are not explicitly closed will not be added or returned to the pool.
Remove Connection
If the connection life has passed or the pool Manager detects that the connection to the server has been disconnected, the object pool manager
The connection is removed from the pool. Note that this type of information can be detected only when you try to communicate with the server.
Status. If a connection is no longer connected to the server, it is marked as invalid. The object pool manager determines
Scan the connection pool to find the objects that have been released to the pool and marked as invalid. The connections will be permanently
Remove for a long time.
If the connection still exists with the disappearing server, even if the object pool manager does not detect the disconnected connection and
Mark it as invalid and may still be removed from the pool. In this case, an exception is generated. However
Yes. To release the connection back to the pool, you must disable it.