In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the JSP database connection pool example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
In general, when using to develop database-based WEB programs, the traditional pattern is basically as follows:
1. Establish a database connection in the main program (such as Servlet, Beans).
2. Perform SQL operation to extract data.
3. Disconnect the database.
There are many problems in using this model for development. First of all, we need to establish a JSP database connection for each WEB request (for example, viewing the contents of an article). For one or more operations, you may not be aware of the overhead of the system, but for WEB programs, even in a short period of time, the number of operation requests is far from one or two. But dozens of times (think of the world's netizens are likely to find information on your page), in this case, the system overhead is quite high. In fact, in a database-based WEB system, establishing a database connection will be one of the costly operations in the system. In many cases, this may be the bottleneck in the speed of your website.
Second, using the traditional mode, you have to manage each connection to ensure that they are closed correctly. If a program exception causes some connections to fail to close, it will result in a memory leak in the database system. Eventually we will have to restart the database.
In view of the above problems, we first think that we can use a global Connection object, which will not be closed after creation, and the program will use it all the time, so that there is no problem of creating and closing the connection every time. However, if the same connection is used too many times, it will lead to the instability of the connection, which will lead to the frequent restart of WEB SERVER. Therefore, this method is not advisable. In fact, we can use connection pooling technology to solve the above problems. First of all, introduce the basic principles of connection pooling technology. As the name implies, the basic idea of connection pooling is to establish some connections in advance and place them in memory objects for use:
When you need to establish a database connection in a program, you only need to take one from memory instead of creating a new one. Similarly, after use, you just need to put it back into memory. The establishment and disconnection of connections are managed by the connection pool itself. At the same time, we can also set the parameters of the connection pool to control the number of connections in the connection pool, the number of times each connection is used, and so on. Through the use of connection pooling, the program efficiency will be greatly improved. at the same time, we can monitor the number and usage of database connections through its own management mechanism.
Let's take a connection pool called ConnectionPool as an example to look at the implementation of the connection pool. Let's first look at the basic properties of ConnectionPool:
M_ConnectionPoolSize: the lower limit of the number of connections in the connection pool m_ConnectionPoolMax: the upper limit of the number of connections in the connection pool m_ConnectionUseCount: the number of times a connection is used m_ConnectionTimeout: the maximum idle time of a connection m_MaxConnections =-1: the number of * connections at the same time m_timer: timer
These properties define the valid status values for the connection pool and each connection in it. In fact, the self-management of connection pool is to judge the state and number of each connection regularly. The management process is as follows:
We can define the basic interfaces that ConnectionPool needs to complete management:
Public class ConnectionPool implements TimerListener {public boolean initialize () / / connection pool initialization public void destroy () / / destroy a connection pool public synchronized java.sql.Connection getConnection () / take a connection public synchronized void close () / / close a connection private synchronized void removeFromPool () / / remove a connection from the connection pool private synchronized void fillPool () / / maintain the connection pool size public synchronized void TimerEvent () / / timer event handler function}
Through these interfaces, the basic management of connection pool can be completed. Complete the state verification of the JSP database connection pool in the TimeEvent () function. When fillPool (), the connection pool at least keeps the minimum number of connections. Because we want to save the state of each connection, we also need a database connection object:
Class ConnectionObject {public java.sql.Connection con; public boolean inUse; / / whether the flag public long lastAccess; / / the last start time public int useCount; / / the number of times used}
With the addition of the ConnectionObject object, ConnectionObject should be the only operation in ConnectionPool, while all other processes need
The con property of ConnectionObject, so we add another class as the interface for other processes to obtain and return connections: CLASS Conn {GetConnection (); / take a valid connection CloseConnection () from the connection pool; / / return the connection without closing the connection, just put back the connection pool DestroyPool () / / destroy connection Pool} Thank you for reading this article carefully. I hope the article "sample Analysis of JSP Database connection Pool" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.