Recently I made the frequent connection of database JSP, which gives the need to the database connection pool. For JSP, a good J2EE server is necessary, JBOOS, WebLogic is a good solution.
In general, the traditional mode is basically followed by using the development of a database-based web program.
1. A database connection is created in the main program (such as servlet, beans).
2. Perform SQL operations and remove data.
3. Disconnect the database connection.
Use this model development, there are many problems. First, we have to establish a database connection for each web request (such as the content of an article). For one or more operations, you may not be able to expect the system overhead, but for the web program, Even in a short period of time, the number of operations is far from one or two, but dozens of hundreds of times (thinking about users around the world may find information on your web), In this case, the system overhead is quite large. In fact, in a database-based Web system, the operation of establishing a database connection will be one of the largest operations in the system. Many times, it is possible that your website speed bottleneck is here.
Second, use the traditional mode, you must manage each connection, make sure they can be properly closed, if the program exception occurs, some connections have not been closed, will cause memory leak in the database system, and ultimately we will have to restart database.
For the above questions, we first think of using a global Connection object. It is not closed after being created. After the program has been using it, there is no problem with each creation, close the connection. However, the same connection number is too much, which will result in unstable connectivity, which in turn causes the frequent frequency restart of the web server. Therefore, this method is not available. In fact, we can use the connection pool technology to solve the above problems. First, introduce the basic principles of connecting pool technology. As the name suggests, the most basic idea of connecting pool is to pre-establish some connections in memory objects for use:
As shown, when a database connection is required in the program, it is only necessary to take one from memory without new construction. Similarly, after use, just put it back in memory. And the establishment of the connection, there is a connection pool itself to manage it. At the same time, we can also control the number of connections in the connection pool by setting the parameters of the connection pool, the maximum number of usage, and the like. By using the connection pool, we will greatly improve program efficiency, and we can monitor the number of database connections, usage, and so on through its own management mechanism. Below we take a look at the connection pool as an example with a connection pool called ConnectionPool. Let's take a look at the basic properties of ConnectionPool:
M_ConnectionPoolSize: Lower connection in the connection pool
M_ConnectionPoolmax: The upper limit of the connection in the connection pool
M_ConnectionUsecount: Maximum number of connections
M_ConnectionTIMEOUT: The longest free 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 to each of them. The self-management of the connection pool is actually determined by the number of connections, the number of connections to each connection, and performs the corresponding operation. The management process is as follows:
With the above figure, we can define the basic interface to complete the management needs:
Public Class ConnectionPool Implements Timerlistener {Public Boolean Initialize () // Connection Pool Initialization
Public void destroy () // Connect the pool destruction
Public synchronized java.sql.connection getConnection () // Take a connection
Public synchronized void close () // Close a connection
Private synchronized void transovefromPool () // Remove a connection from the connection pool
Private synchronized void fillpool () // Maintenance connection pool size
Public synchronized void timeevent () // Timer event handler
}
Through these interfaces, the basic management of the connection pool can be completed. When you complete the status verification of the connection pool in the TimeEvent () function, the connection pool maintains at least the maximum number of connections when FillPool (). Because we have to save the status of each connection, you still need a database connection object:
Class connectionObject {
Public java.sql.connection con; // is used to use a sign
Public long lastaccess; // Last started using time
Public int usecount; // is used
}
After adding the ConnectionObject object, the operation in ConnectionPool should just be only ConnectionObject, and other processes need only the ConnectionObject's CON attribute, so we join a class, which is used as other processes to return the interface: Class conn {
GetConnection (); // Remove an effective connection from the connection pool
CloseConnection (); // Returns the connection, there is no connection, just put it back the connection pool
DestroyPool (); // Destroy the connection pool
}
Finally, our overall system architecture is as follows:
Through the above introduction, we can see that the key to connecting pool technology is its own management mechanism. The above management process is only a point of view, the key is to introduce one idea to you. On this basis, you can further improve Connecting pool technology for you.