One, Connection pool
1. What is a connection pool? Why do we need it?
Connection pooling allows multiple clients to use cached connection objects that can connect to the database, which are shared and reusable.
Open/Close database connection is expensive, connection pooling technology allows us to maintain connection objects in the connection pool, which can improve the performance of the database execution commands. Multiple client requests can reuse the same connection object, and each time a client request is received,
The connection pool is searched to see if there are idle connection objects. If not, either all client requests are queued up, or a new connection object is created in the pool (depending on how many connections already exist in the pool and how many connections are supported by the configuration).
Once a request has finished using the Connection object, the object is re-placed in the pool and then reassigned to the queued waiting request (which request is dispatched to see what scheduling algorithm is used).
Because most requests are using existing connection objects, connection pooling technology greatly reduces the time it takes to wait to create a database connection, reducing the average connection time.
2. How do I use connection pooling?
Connection pooling is common in network-based enterprise applications where the application server is responsible for creating connection objects, adding them to the connection pool, assigning connection objects to requests, reclaiming used connection objects, and re-putting them back into the connection pool.
When a network application creates a database connection, the application server pulls the connection object out of the pool, and when it is closed, the application server is responsible for putting the used connection object back into the pool.
PS: You can also use the JDBC 1.0/JDBC 2.0 API to get physical connections (physical connnection), but this is rare because the database only needs to be connected once and no connection pooling is required.
3. How many connections can the connection pool handle? Who creates/releases the connection?
You can configure the maximum number of connections, the minimum number of connections, the maximum number of idle connections, and so on, all of which can be configured by the server administrator. When the server starts, a fixed number of connection objects (the minimum number of connections configured) are created and added to the connection pool.
When client requests consume all of the connection objects, new requests are made to create new connection objects, which are added to the connection pool and then assigned to the new request until the maximum number of connections is set.
The server also keeps viewing the number of idle connection objects, and when the number of unused connections is detected that exceeds the set value, the server shuts down idle connections, and then they are garbage collected.
4. Traditional connection pool vs manageable connection pool
Connection pooling is an open concept, and any application can use this concept and manage it in the way it wants. The connection pooling concept refers to creating, managing, and maintaining connection objects.
But when the scale of the application increases, it becomes increasingly difficult to manage connections without a robust connection pooling mechanism.
Therefore, it is necessary to build a robust, manageable pool of connections.
PS: About connection pool content, refer to from http://www.importnew.com/8179.html
Threads & thread pool, connection & connection pool
threads : The smallest unit of a program execution flow, an entity in a process, a relatively independent, scheduled execution unit, is the basic unit of the system's independent dispatch and dispatch;
Multithreading technology, refers to in a process can create multiple threads "simultaneously" processing multiple transactions;
Thread Pool : Can be understood as a buffer, because the frequent creation of the destruction of the thread will bring a certain cost, can be pre-created, but not immediately destroyed, to share the service for others, one can provide efficiency, but also control the thread wireless expansion.
Connection : A point of connection with another point;
connection Pooling : The same beauty as the thread pool, but the connection pool can be based on multithreading, can be implemented by multiple processes, or it may be single-instance.
As an example:
Socket in the service, can listen to multiple client connections at the same time, then its implementation principle is a bit like "connection pool", each customer through multiple ports at the same time to the server to send data, can be considered multi-threading,
The server may have established n threads to wait to process/analyze the data sent by the client at the same time, but there is a "thread pool".
Talk about connection pooling and threading