In generalProgramIn the traditional mode, follow these steps:
1. Create a database connection in the main program (such as Servlet and beans.
2. perform SQL operations to retrieve data.
3. Disconnect the database.
There are many problems when using this mode for development. First, we need to viewArticleFor one or several operations, you may not be aware of the system overhead. However, for web programs, even in a short period of time, the number of Operation requests is far from one or two, but dozens or even hundreds of times (think that all netizens around the world may find information on your webpage). In this case, the system overhead is quite large.
In fact, in a database-based web system, database connection is one of the most costly operations in the system. Most of the time, this may be the bottleneck for your website speed.
Second, in the traditional mode, you must manage each connection to ensure that they can be properly closed. If a program exception occurs and some connections fail to be closed, memory leakage in the database system will occur, in the end, we will have to restart the database.
To solve the above problems, we first thought we could use a global connection object. After the object is created, it will not be closed, and the program will continue to use it, so that there will be no problem of creating or closing the connection each time. However, if the same connection is used too many times, the connection will be unstable and the web server will frequently restart.
Therefore, this method is not advisable. In fact, we can use the connection pool technology to solve the above problems. First, introduce the basic principles of connection pool technology. As the name implies, the most basic idea of a connection pool is to pre-establish some connections and place them in memory objects for use:
To establish a database connection in a program, you only need to obtain one from the memory instead of creating a new one. Similarly, after use, you only need to put back the memory. The connection pool is used to manage the connection establishment and disconnection. At the same time, we can also set the connection pool parameters to control the number of connections in the connection pool, the maximum number of times each connection is used, and so on. By using the connection pool, we can greatly improve program efficiency. At the same time, we can monitor the number and usage of database connections through its own management mechanism. Next we will take a connection pool named connectionpool as an example to look at the implementation of the connection pool. Let's take a look at the basic attributes of connectionpool:
M_connectionpoolsize:
Minimum number of connections in the connection pool
M_connectionpoolmax:
Maximum number of connections in the connection pool
M_connectionusecount:
Maximum number of times a connection is used.
M_connectiontimeout:
Maximum idle time of a connection
M_maxconnections =-1:
Maximum number of connections at the same time
M_timer: Timer
These attributes define the valid status values of the connection pool and each connection in it. The self-management of the connection pool is to determine the status and number of connections of each connection regularly. The management process is as follows:
Through this, we can define the basic interfaces required for connectionpool to complete management:
Public class connectionpool
Implements timerlistener
{
Public Boolean initialize ()
// Connection pool Initialization
Public void destroy ()
// Destroy the connection pool
Public synchronized java. SQL. Connection
Getconnection ()
// Obtain a connection
Public synchronized void close ()
// Close a connection
Private synchronized
Void removefrompool ()
// Delete a connection from the connection pool
Private synchronized
Void fillpool ()
// Maintain the connection pool size
Public synchronized
Void timerevent ()
// Timer event processing function
}
Through these interfaces, you can complete basic management of the connection pool. The status check of the connection pool is completed in the timeevent () function. When fillpool () is used, the connection pool maintains at least the minimum number of connections. Because we want to save the status of each connection, we also need a database connection object:
Class connectionobject
{
Public java. SQL. Connection con;
Public Boolean inuse;
// Whether the flag is used
Public long lastaccess;
// Last start time
Public int usecount;
// Number of times used
}
After a connectionobject object is added, only connectionobject is operated in the connectionpool, and all other processes need is the con attribute of the connectionobject. Therefore, we add another class, as an interface for other processes to obtain the connection to the returned result:
Class Conn
{
Getconnection ();
// Extract a valid connection from the connection pool
Closeconnection ();
// Return the connection. The connection is not closed at this time, but the connection pool is replaced.
Destroypool ();
// Destroy the connection pool
}
Finally, our overall system architecture is as follows: