Several mainstream Java connection pool grooming

Source: Internet
Author: User
Tags connection pooling

Pool technology can significantly optimize the performance of server applications, improve program execution efficiency and reduce system resource overhead. The pool described here is a generalized pool, such as database connection pool, thread pool, memory pool, object pool, and so on. The object pool can be seen as a container for saving objects, and a certain number of objects are created when the process is initialized. When needed, a free object is removed directly from the pool, and the object is not disposed of directly, but placed in the object pool to facilitate the next object request to be reused directly. The same is true of several other pools, and the advantage of pool technology is that it can eliminate the delay caused by object creation and thus improve the performance of the system.

To understand Java connection pooling let's first understand the principle of database connection pooling (connection pool), where Java connection pooling is the application of database connection pooling in Java. --we know that there is a well-known design pattern for shared resources: resource pools (Resource pool).

This model is to solve the problem caused by the frequent allocation of resources ﹑ release. To solve the above problems, the database connection pooling technology can be adopted. The basic idea of a database connection pool is to establish a "buffer pool" for database connections. A certain number of connections are pre-placed in the buffer pool, and when a database connection needs to be established, simply take one out of the buffer pool and put it back when you are finished. We can prevent the system from endlessly connecting to the database by setting the maximum number of connections to the connection pool. More importantly, we can monitor the number of connections in the database through the connection pool management mechanism ﹑ usage, provide the basis for system development ﹑ test and performance adjustment.

C3P0 is an open source JDBC connection pool, which is published in the Lib directory with Hibernate, including statement objects that implement the connection and datasources pools of JDBC3 and JDBC2 extension specification descriptions. (Home: http://sourceforge.net/projects/c3p0/)

BONECP is an open source, fast JDBC connection pool. BONECP is very small, only more than 40 K (runtime needs log4j and Google Collections support, which add up is not small), compared to c3p0 more than 600 K. Another person thinks that BONECP has a disadvantage is that the JDBC driver is loaded outside the connection pool, so that some application server configuration is not flexible. Of course, small volume is not bonecp excellent reason, BONECP in the end there is what prominent place, please see Performance test report. (Home: http://jolbox.com/)

DBCP (Database Connection pool) is a pool of databases that relies on the Jakarta Commons-pool object pooling mechanism, and the Tomcat data source uses DBCP. Currently, there are two versions of DBCP, 1.3 and 1.4, respectively. Version 1.3 corresponds to JDK 1.4-1.5 and JDBC 3, while the 1.4 version corresponds to JDK 1.6 and JDBC 4. So when choosing a version, look at what JDK version you're using, and there's no difference in functionality. (Home: http://commons.apache.org/dbcp/)

Proxool is a Java SQL driver driver that provides a connection pooling package for other types of drivers that you choose. Can be ported to existing code very simply. Fully configurable. Fast, mature and robust. You can transparently increase the connection pooling capability for your existing JDBC driver.

Database connectivity is a critical, limited, and expensive resource that is particularly prominent in multi-user Web applications. The management of database connections can significantly affect the scalability and robustness of the entire application, affecting the performance of the program. The database connection pool was raised for this issue. The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing a database connection that has been idle for more than the maximum idle time to avoid missing database connections due to the absence of a database connection being freed. This technology can significantly improve the performance of database operations.

Common database connection Pools in Java are: DBCP, c3p0, BONECP, Proxool, Ddconnectionbroker, Dbpool, Xapool, Primrose, Smartpool, Miniconnectionpoolmanager and Druid and so on.

Java Open source data connection pool: http://www.open-open.com/20.htm

Hibernate common configuration for three connection pools: http://tieba.baidu.com/f?kz=70604644

Several common Java connection pools: http://hi.baidu.com/tangyudee/blog/item/f8bdb43decca892571cf6ced.html

I feel it is necessary to elaborate some concepts of connection pool before introduction, which will help to understand some of the words behind.
The most primitive use of the database is to open a connection and use it, after use must close the connection to release resources. Due to frequent open and close connections to the JVM including the database
Have a certain resource load, especially when the use of high pressure resource consumption is more prone to produce performance problems. The use of the connection pool is manifested by the fact that his principle is not complex:
First open a certain number of database connections, when the use of the time allocated to the caller, after the call is returned to the connection pool, note that after the connection pool is returned, these connections are not closed, but
Ready to assign to the next caller. It can be seen that the connection pool saves a large number of database connection opening and closing actions, the benefits of system performance is self-evident.
Several concepts:
Minimum connection-The number of connections that are opened after the app starts and the number of connections that are subsequently minimized.
Maximum number of connections-the maximum number of connections the app can use
Connection growth number-the number of new open connections applied each time
For example, the operation of a connection pool is as follows:
Assuming that the minimum and maximum connections are set to 10, 20, then the application will open 10 database connections as soon as it starts, but note that the database connection pool is using a number of 0--because you are not using these connections and the number of idle is 10. Then you start to log in, assuming that the login code uses a connection to query, then the database connection pool is using a number of 1, the idle number is 9, this does not need to open the connection from the database-because the connection pool is ready to leave 10 to you. What is the number of connections to the current connection pool at the end of the login? 0, of course, because that connection has been returned to the connection pool as the transaction ends. Then there are 11 people logged in in the same second, what happens: Connection pool from the database new application (open) a connection, together with the other 10 to send out, this instantaneous connection pool of the number of uses is 11, but it is not normal to be in a moment will become 0. What if there are 21 people logged in at the same time? The 21st person can only wait for someone in front of the login and then release the connection to him. At this point the connection pool is open with 20 database connections-although it is likely that the number has been reduced to 0, will the 20 connections remain? Of course no, the connection pool will close a certain amount of connection to the database within a certain time, in this case the number is 20-10=10, because only need to keep the minimum number of connections, and this time period is also configured in the connection pool.

Well, the basic concept is over, and the main idea is to compare it.
As a first step, it is recommended that you use Jndi to unify the configuration of the connection pool in order to apply portability and configurable angles. How to configure in fact, there are many examples on the internet,
Here's a quick example (using the Spring Framework):
First configure the Jndi name in the context definition of the app, such as a resource.xml file, inside the syntax
<bean id= "DataSource" class= "Org.springframework.jndi.JndiObjectFactoryBean" >
<property name= "Jndiname" ><value>jdbc/myapp</value></property>
</bean>
Note DataSource This bean needs to be configured in the DAO layer (Hibernate or JDBC) configuration file as the datasource name in all beans
where "Jdbc/myds" is the Jndi name, the next step is to connect the database in the application Server connection pool and the corresponding Jndi configuration

An open source data connection pool
1 DBCP
DBCP may be the most used open source connection pool, probably because of the ease of configuration, and many examples of open source and Tomcat applications are used for this connection pool.
This connection pool can set the maximum and minimum connections, connection waiting time, etc., basic functions are available. The configuration of this connection pool is described in the attachment compression package: Dbcp.xml
Usage Evaluation: In the specific project application, it is found that the stability of the continuous operation of this connection pool is still possible, but the speed is slightly slower, the stability under the pressure of the large concurrent quantity
The connection pool monitoring is also not available

2 C3P0
C3P0 is another open source connection pool, in the industry is also more famous, this connection pool can set the maximum and minimum connection, connection waiting time, etc., basic functions have.
The configuration of this connection pool is described in the attachment compression package: C3p0.xml.
Use evaluation: In the specific project application, found that the continuous operation of this connection pool stability is quite good, under the pressure of large concurrent volume of stability is also guaranteed,
Connection pool monitoring is also not available.

3 Proxool
Proxool This connection pool may use less people, but also have a certain reputation, this connection pool can be set maximum and minimum connection, connection waiting time, etc., basic functions have.
The configuration of this connection pool is described in the attachment compression package: Proxool.xml.
Usage Evaluation: In a specific project application, it is found that the stability of the continuous operation of this connection pool has some problems, there is a task scenario task that requires a long run of batch, the same code
Ended successfully in another 2 open source connection pool, but an exception exited in Proxool.
But Proxool has one advantage--connection pooling monitoring, which is a tempting thing to do, and the approximate configuration is to add the following definition to Web. xml:
<servlet>
<servlet-name>admin</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
and access after app launch: http://localhost:8080/myapp/admin this URL to monitor
However, Proxool's own package in the monitoring use will have a coding problem, the attachment has a
For the package that resolves this issue, see the following: Proxool-0.9.0rc3.jar in the attachment compression package. Also need to jdk1.5 above the environment.

Summary moment:
In summary, these several open source connection pool each has the merits and demerits, the recommendation uses the C3P0, has tested this kind of connection pool performance is stable, the pressure ability is strong. And Proxool Despite the obvious performance problems,
However, because it has monitoring capabilities, it is recommended to use during development testing to help determine if there is a connection not being turned off and to exclude some of the code's performance issues.

Two commercial middleware Connection pool
There are several open-source connection pools listed above, in fact, if the conditions allow the use of middleware such as WebLogic and WebSphere, then do not hesitate, be sure to
Using these business-level middleware to bring the database connection pool, their performance and deployment and open source is not a magnitude, for example, there was a project, the volume of data is large, the same code application, in 3 open-source connection pool has been the number of system anomalies, The connection pool of WebLogic and WebSphere runs normally, of course, later found that the code has some flaws, but also the side of the strong business connection pool.

1 WebLogic Connection Pool
WebLogic 8 is a easy-to-use Application Server software, but to 9 is simply torture, do not know what Bea think, Oracle acquired BEA after 10, more than 9, but most like the 8 ...
Aside from that, let's introduce his database connection pool in 8.1 version (actually 10 of the configuration is similar)
The first is the basic settings of the connection pool, this does not speak, there are many tutorials on the web. Then go to the Data Sources menu to configure the Jndi Name inside the DataSource, and the same as before in the application configuration: Jdbc/myapp.
Then there are the configuration of the connection pool for some specific items, including setting the minimum (Initial capacity), maximum (Maximum capacity) connection, and
How many connections (capacity Increment) need to be increased each time the connection is increased. Allow Shrinking (whether to return unused connections to the database to keep the minimum number of connections-this can be understood in the previous example of connection pool elaboration).
There are also several interesting options test Reserved Connections: Testing the connection made, test released Connections: Testing the released connection. Someone will ask, what's the use of this?
Do not know everyone in the project encountered the Java report connection failure exception, anyway, I have encountered, only when the system pressure to appear. With this option you don't have to worry about it-because the connection pool has been tested for you, and once you check that the connection is invalid, he will discard it back to the database and only give you a valid one. However, this connection failure of the exception is mostly due to the application is not rigorous, we are more concerned about the problem of the code--but at least WebLogic thought to help you make up, is not very intimate:)
Another important feature of course is the connection pool monitoring: Monitor tab can see the use of the situation, some people have to ask, there is no indicator ah, do not forget the custom view this feature link OH:)
The following metrics are available: Current number of connections, peaks that have been reached, number of connections that can be used, number of connections waiting, number of connections opened from the database, number of connections that have been closed ... Top 3 of them are my top concerns.

Usage Evaluation: In the specific project application, the continuous operation of this connection pool is very stable, the performance is very good under the pressure of large concurrency, in addition, in some cases, connection pool connection can be released in time.
Connection pool Monitoring at a glance, timely in place.

2 WebSphere Connection Pool
Or do you want to take a first paragraph digression: Remember someone said that WebSphere only after version 6 is WebSphere, personally agree. WebSphere 5 and Previous versions ... Let's just forget it.
In fact, WebSphere's connection pool adheres to IBM's consistent style: powerful, complex to use:)
Access to the console using the "JDBC Provider" feature menu for connection pooling Basic configuration, all the way down, different database configuration method is not the same, the strangest thing is to manually add the user and password parameters, if not
The information guide is really confusing. These basic settings are still online to find a lot of. Connection pooling settings you also need to set up the data source, as the Jndi name corresponds to the previous: Jdbc/myapp
Advanced settings include initializing the number of connections, maximum connections, connection validity checks, and not using timeouts. In fact, these are roughly the same as the connection pool configuration in WebLogic.
Connection pool monitoring: Using the Run Monitoring menu, there will be a monitoring project selection, the selection of JDBC monitoring can be, hateful is the beginning of what the server operating system needs to install what the graphical control, the choice is to find the control installed under the operating system (Linux), and then a lot of dependent components are not ... It took a while to find the choice is not, monitoring data as well as graphics can come out, really want to anger.
Despite the twists and turns but the monitoring content is very powerful, as in the connection pool, including the current number of connections, the peak that has been reached, the number of connections that can be used, the number of connections opened from the database, the number of connections that have been closed ... One of the top 3 things I'm most concerned about is the strange fact that the number of connections that have been turned on since the start of the application has not reached the initial defined number of connections, and it's unclear how WebSphere is a computational mechanism.
In addition, when the pressure is high, the number of connections can be negative, it was very strange, think about it, the negative number must be queued to wait for the quantity

Use evaluation: In the specific project application, the continuous operation of this connection pool stability is quite strong, under the pressure of large concurrency is good enough, in addition, in some cases connection pool connection can be released in time,
The connection pool monitoring configuration is somewhat complex, but the indicators are at a glance and graphically displayed when configured.

Summary moment:
I was impressed by the two business-level connection pools, powerful, stable, excellent performance, and monitoring in place. It can be said that difficult to divide, relatively weblogic connection pool using configuration and monitoring configuration is simpler and clearer, and websphere more complex but more options function. In fact, this is a direct reflection of the use of the two application servers, of course, the overall comparison of 2 application servers should be another topic, perhaps in the next issue of the content ...

Compare the advantages and disadvantages of a variety of connection pools, the following topic may not be directly related to the comparison itself, but personally think it should be more valuable experience to share it, that is---so many indicators configuration, those maximum and minimum number of connections and some other necessary configuration indicators, What values should be configured in a formal production project?
In fact, this value is based on the specific project situation, data size and concurrency (though it seems like a cliché, but the rigorous style of our research and development staff is necessary:). Specifically in the medium-sized small project-give a numerical value, the number of users 300 to 3000, the amount of data 1 million to 100 million---, it is recommended that WebLogic set to maximum and minimum is 200,websphere min 200 max 300, if 2 is set to the minimum memory above 1G , of course, if the condition allows the larger memory, the better, but the 32-bit machine memory 1.5G limit is certain (64 bit I would like to set a 4G memory, the speed of the feeling is very cool AH). When this figure comes out, I believe there will be a lot of problems to be thrown over, and I'll talk about my experiences and thoughts.
1 Why is 200 or 300 instead of higher?
Answer: Redistribution more effect is not big, one is the application server to maintain the number of connections need memory support, just said the 32-bit machine can only support to 1.5G, and maintain a large number of connections to allocate the use of the CPU is also a small load, so it should not be too large.
2 Why not a little bit?
Answer: If it is too small, then the amount of concurrency in the above-mentioned projects and the amount of data will cause queuing phenomenon, the system will become slow, the database connection will be open and close frequently, performance pressure, user experience is not good.
3 why WebLogic the smallest and largest are the same, and WebSphere is not the same
Answer: In fact, and allocate memory of the minimum maximum value of the same situation, generally recommended 2 values should be consistent, all put in memory just fine. But IBM's official recommendation 2 values to be distinguished---the official word is still to be heard.
4 allocation of other open source connection pools haven't said yet?
Answer: The open source of the individual think to 100, and then high he will not be too stable, of course, 1G of the minimum memory is to give Tomcat points
5 Are there any other indicators?
Answer: Of course, there are some, but personally think the rest of the specific situation is not listed here, we are interested to discuss and share with me.

Several mainstream Java connection pool grooming

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.