Summary
The connection pool allows the application to get a connection from the connection pool and use this connection without having to re-establish a connection for each connection request. Once a new connection is created and placed in the connection pool, the application can reuse this connection without having to implement the entire database connection creation process.
When the application requests a connection, the connection pool assigns a connection to the application instead of re-establishing a connection; when the application is connected, the connection is returned to the connection pool instead of direct release.
How to implement a connection pool
Make sure you use the same connection string with the same connection (and the connection pool); the connection pool will work only when the connection string is the same. If the connection string is different, the application does not use the connection pool but create a new connection.
advantage
The most important advantage of using the connection pool is performance. Creating a new database connection is mainly dependent on the speed of the network and the distance from the application and database server, and this process is usually a very time consuming process. After using the database connection pool, the database connection request can be renewed directly by the connection pool without need to reconnect, authenticate to the database server, which saves time.
Disadvantage
There may be multiple connections that have not been used in the database connection pool (this means waste of resources).
Tips and prompts
1. When you need a database connection, you will create a connection pool instead of establishing in advance. Once you use the connection to close it immediately, don't wait until the garbage collector is to process it.
2. Make sure all user-defined transactions are turned off before turning off the database connection.
3. Do not close all connections in the database, at least if there is a connection in the connection pool available. If memory and other resources are issues you must first consider, you can close all connections and create a connection pool when the next request arrives.
Connection pool FAQ
1. When will I create a connection pool?
When the first connection request comes, the connection pool is created; the connection pool is established by the connection character of the database. Each connection pool is related to a different connection string. When a new connection request is the same as the string used by the connection string and the connection pool, take a connection from the connection pool; if not the same, create a new connection pool.
2. When is it close to the connection pool?
The connection pool is turned off when all connections in the connecting pool have been closed.
3. What happens when connecting in the connection pool is used up, and what happens when a new connection request comes?
When the connection pool has reached its maximum connection number, new connection requests are placed in the connection queue when there is a new connection request. When there is a connection release to the connection pool, the connection pool allocates the newly released connection to the connection request queued in the queue. You can call Close and Dispose to return the connection to the connection pool.
4. How should I allow the connection pool?
For .NET applications, the connection pool is permitted by default. (This means you don't have to do anything for this matter).
5. How should I prohibit connecting pools?
ADO.NET defaults to allow database connection pools, if you want to prohibit connecting pools, you can use the following way:
1) When using the SqlConnection object, the connection string is as follows: pooling = false;
2) When using the OLEDBConnection object, the connection string is added to the following: OLE DB Services = -4;