Necessity of JSP database connection pool

Source: Internet
Author: User
Recently I have made frequent connections to the database JSP, and the necessity of the database connection pool is given here. For JSP, a good J2EE server is necessary. jboos and WebLogic are both good solutions.

In general, when developing database-based web programs, the traditional mode basically follows the steps below:
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 establish a database connection for each Web Request (for example, checking the content of an article). For one or more operations, you may not be aware of the system overhead, however, for a web program, even in a short period of time, the number of Operation requests is far from one or two, it is more than a dozen or even hundreds of times (think that users all over the world may search for 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: the 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 () // get 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, but the connection pool is replaced.
Destroypool (); // destroy the connection pool
}
Finally, our overall system architecture is as follows:
     

Through the above introduction, we can see that the key to the connection pool technology is its own management mechanism. The above management process is just my opinion, and the key is to introduce a way of thinking to everyone, on this basis, you can further improve the connection pool technology for your use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.