View the necessity of J2EE server and connection pool from the connection of JSP Database

Source: Internet
Author: User

Recently, I have made frequent connections to the JSP database, 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, such as viewing the content of a certain 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, 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 JSP database 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:

 
 
  1. Public ClassConnectionPoolImplementsTimerListener {
  2. Public BooleanInitialize ()// Connection pool Initialization 
  3. Public VoidDestroy ()// Destroy the connection pool 
  4. Public SynchronizedJava. SQL. Connection getConnection ()// Obtain a connection 
  5. Public Synchronized VoidClose ()// Close a connection 
  6. Private Synchronized VoidRemoveFromPool ()// Delete a connection from the connection pool 
  7. Private Synchronized VoidFillPool ()// Maintain the connection pool size 
  8. Public Synchronized VoidTimerEvent ()// Timer event processing function 
  9. }

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:

 
 
  1. ClassConnectionObject {
  2. PublicJava. SQL. Connection con;Public BooleanInUse;// Whether the flag is used 
  3. Public LongLastAccess;// Last start time 
  4. Public IntUseCount;// Number of times used 
  5. }

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:

 
 
  1. CLASS Conn {
  2. GetConnection ();// Extract a valid connection from the connection pool 
  3. CloseConnection ();// Return the connection. The connection is not closed at this time, but the connection pool is replaced. 
  4. DestroyPool ();// Destroy the connection pool 
  5. }

Finally, the overall architecture of our JSP database system is as follows:

 

Through the above introduction, we can see that the key of connection pool technology is its own management mechanism. The above JSP database management process is just my opinion. The key is to introduce you to an idea. On this basis, you can further improve the connection pool technology for your use.

  1. How to connect to MySQL database in JSP
  2. Connect to a database using JDBC
  3. Get database connection in JSP
  4. Necessity of JSP database connection pool
  5. JSP Database Operations routine JDBC-ODBC)

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.