Sort out several mainstream Java connection pools

Source: Internet
Author: User

To a certain extent, the pool technology can significantly optimize the performance of server applications, improve program execution efficiency and reduce system resource overhead. The pool mentioned here is a pool in a broad sense, such as the database connection pool, thread pool, memory pool, and Object pool. The object pool can be seen as a container for saving objects. A certain number of objects are created during process initialization. When needed, a free object is taken directly from the pool. When used up, the object is not directly released, but is placed in the object pool to facilitate the next object request to be reused directly. The same is true for the design philosophy of other pools. The advantage of the pool technology is that it can eliminate the latency caused by object creation and thus improve the system performance.

To understand the Java connection pool, we must first understand the principles of the database connection pool. The Java connection pool is the application of the database connection pool on java. -- We know that there is a well-known design pattern for shared resources: resource pool ).

This mode is designed to solve the problems caused by frequent resource allocation and release. To solve the above problems, you can use the database connection pool technology. The basic idea of the database connection pool is to create a "buffer pool" for the database connection ". A certain number of connections are put in the buffer pool in advance. When you need to establish a database connection, you only need to extract one from the "buffer pool" and put it back after use. We can set the maximum number of connections in the connection pool to prevent endless connections to the database. More importantly, we can monitor the number and usage of database connections through the connection pool management mechanism to provide a basis for system development, testing and performance adjustment.

C3p0 is an open-source JDBC connection pool. It is released together with hibernate in the lib directory, including datasources objects for connection and statement pools that implement the jdbc3 and jdbc2 extension specifications. (Home: http://sourceforge.net/projects/c3p0)

Bonecp is an open-source and fast JDBC connection pool. Bonecp is very small and only has 40 K (log4j and Google collections are required for running, and the two are not small). In contrast, c3p0 requires more than six hundred K. In addition, I personally think the disadvantage of bonecp is that the JDBC driver is loaded out of the connection pool, so the configuration of some application servers is not flexible enough. Of course, the small size is not why bonecp is excellent. What are the highlights of bonecp? Please refer to the performance test report. (Home: http://jolbox.com /)

DBCP (database connection pool) is a database connection pool dependent on the Jakarta commons-pool Object pool mechanism. The Tomcat data source uses DBCP. Currently, DBCP has two versions: 1.3 and 1.4. Version 1.3 corresponds to JDK 1.4-1.5 and JDBC 3, while version 1.4 corresponds to JDK 1.6 and JDBC 4. Therefore, when selecting a version, you need to check which JDK version you are using. There is no difference in functionality. (Home: http://commons.apache.org/dbcp)

Proxool is a Java SQL driver that encapsulates the connection pool of other types of drivers you choose. It can be easily transplanted to the existing code. Completely configurable. Fast, mature, and robust. You can transparently Add the connection pool function to your existing JDBC driver.

Database connection is a key, limited, and expensive resource, which is particularly prominent in the Web applications of multiple users. Database connection management can significantly affect the scalability and robustness of the entire application, and affect the performance indicators of the program. The database connection pool is designed to address this issue. The database connection pool is responsible for allocating, managing, and releasing database connections. It allows applications to repeat an existing database connection instead of reestablishing one; release the database connection with idle time exceeding the maximum idle time to avoid database connection omissions caused by the absence of the database connection. This technology can significantly improve the performance of database operations.

Common Database Connection pools in Java include DBCP, c3p0, bonecp, proxool, ddconnectionbroker, dbpool, xapool, primrose, smartpool, miniconnectionpoolmanager, And Druid.

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

Hibernate commonly used three connection pool configuration: http://tieba.baidu.com/F? Kz= 70604644

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

I feel it is necessary to elaborate on several concepts of the connection pool before the introduction, which will help to understand some of the following words.
The original database is used to open and use a connection. after use, you must close the connection to release resources. Due to frequent opening and closing of connections to JVM, including databases
There is a certain amount of Resource load, especially when the application pressure is large, resource occupation is more likely to cause performance problems. As a result, the function of using the connection pool is shown, and its principle is not complicated:
First, open a certain number of database connections. When used, the connections are allocated to the caller and returned to the connection pool after the call is completed. Note that these connections will not be closed after the connection pool is returned.
Prepare to allocate data to the next caller. It can be seen that the connection pool saves a lot of database connection opening and closing actions, and the benefits of improving system performance are self-evident.
Concepts:
Minimum connection-the number of connections that will be opened immediately after the application is started and the minimum number of connections that will be maintained in the future.
Max connections-Maximum number of connections that an application can use
Connection growth count-Number of new connections that an application opens each time
The following is an example of how the connection pool works:
If the minimum and maximum connections are set to 10, 20, the application first opens 10 database connections once started, note that the number of the database connection pool is 0 at this time, because you are not using these connections, and the number of idle connections is 10. Then you start to log on. Assume that the login code uses a connection for query. In this case, the number of the database connection pool is 1 and the number of idle instances is 9, this does not need to open the connection from the database-because 10 connections have been prepared for you. What is the number of connections in the current connection pool after the logon is over? Of course it is 0, because the connection has been returned to the connection pool as the transaction ends. Then 11 people log on at the same time in the same second. What will happen: the connection pool has a new connection applied for (opened) from the database and sent it together with the other 10, in this instant, the number of connections in the connection pool is 11, but it does not matter. Normally, it will change to 0 in a while. What if 21 people log on at the same time? The first 21st people can only wait for the previous one to log on and release the connection to him. At this time, the connection pool has opened 20 database connections. Although the number of currently used connections has been reduced to 0, will the 20 connections be maintained all the time? Of course not, the connection pool will close a certain amount of connections to the database within a certain period of time. In this example, the number is 20-10 = 10, because you only need to keep the minimum number of connections, this time period is also configured in the connection pool.

Now, the basic concepts have been completed, and we have made a comparison between them.
First of all, we recommend that you use JNDI to configure the connection pool for ease of application transplantation and configuration. In fact, there are many examples of how to configure it online,
Here is a simple example (using the Spring framework ):
Configure the JNDI name in the context definition of the application, such as a resource. xml file.
<Bean id = "datasource" class = "org. springframework. JNDI. jndiobjectfactorybean">
<Property name = "jndiname"> <value> JDBC/MyApp </value> </property>
</Bean>
Note that the datasource Bean must be configured to all beans as the attribute of the datasource name in the configuration file of the DaO layer (hibernate or JDBC ).
The "JDBC/myds" is the JNDI name. The next step is to connect the database and configure the corresponding JNDI in the connection pool of the application server.

I. Open source data connection pool
1 DBCP
DBCP may be the most widely used open source connection pool, probably because it is easy to configure, and many open source and tomcat application examples use this connection pool.
You can set the maximum and minimum connections, connection wait time, and other basic functions for this connection pool. For configuration of this connection pool, see DBCP. XML in the attached compressed package.
Usage evaluation: in a specific project application, it is found that the continuous operation stability of the connection pool is good, but the speed is a little slow, and the stability is under the pressure of a large amount of concurrency.
In addition, connection pool monitoring is not provided.

2 c3p0
C3p0 is another open-source connection pool, which is also well-known in the industry. It can be used to set the maximum and minimum connections, connection wait time, and other basic functions.
For configuration of this connection pool, see c3p0. XML in the attached compressed package.
Usage evaluation: in specific project applications, it is found that the continuous operation stability of the connection pool is quite good, and the stability is also guaranteed under the pressure of a large amount of concurrency,
In addition, connection pool monitoring is not provided.

3 proxool
The proxool connection pool may be used by a small number of people, but it is also well known. This connection pool can be set to the largest and least connections, connection wait time, and other basic functions.
For the configuration of this connection pool, see proxool. XML in the attached compressed package.
Usage evaluation: in a specific project application, it is found that the stability of the continuous operation of the connection pool has some problems. There is a task that requires a long time to run the batch scenario, the same code
In the other two open source connection pools, the connection pool is successfully terminated, but an exception occurs in proxool to exit.
But proxool has an advantage-connection pool monitoring, which is very attractive. The general configuration method is to add the following definitions in 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 the URL http: // localhost: 8080/MyApp/admin after the application is started to monitor
However, the proxool package may be encoded during monitoring. The attachment contains
For packages that solve this problem, see: proxool-0.9.0RC3.jar in the attachment package. In addition, jdk1.5 and later environments are required.

Summary time:
To sum up, each of these open source connection pools has its own advantages and disadvantages. It is recommended that c3p0 be used. It is tested that the connection pool has stable performance and high pressure capacity. Although proxool has obvious performance problems,
However, because it has the monitoring function, it is recommended to use it during development and testing to help determine whether a connection is not closed, and eliminate some code performance problems.

2. Commercial middleware connection pool
Several open-source connection pools are listed above. In fact, if the conditions allow the use of Weblogic, websphere, and other middleware, do not hesitate.
Using the database connection pool provided by these commercial middleware, their performance, allocation, and open source are not in the same magnitude. For example, there was a project with a large data volume, in the same code application, system exceptions occur in the three open-source connection pools, while the connection pools of Weblogic and WebSphere run normally. Of course, the Code may be defective, but it also shows the strength of the commercial connection pool.

1 WebLogic connection pool
WebLogic 8 is an easy-to-use application server software. However, when it reaches 9, it is simply a torment. I Don't Know What Bea thinks. After Oracle acquired Bea, it went 10, better than 9, but the favorite is 8...
If you don't talk about the problem, we will introduce his database connection pool in version 8.1 (in fact, the configuration of 10 is similar)
The first is the basic settings of the connection pool. There are many tutorials on the Internet. Go to the data sources menu to configure the JNDI name in the data source, which must be consistent with that in the previous application configuration: JDBC/MyApp.
The configuration of some specific items in the connection pool, including setting the minimum (initial capacity), maximum (maximum capacity) connection, and
The number of connections (Capacity Increment) that need to be added each time a connection is added ). Allow shrinking (whether to return unused connections to the database to maintain the minimum number of connections-this can be understood by referring to the examples described in the previous connection pool ).
In addition, there are several interesting options test reserved connections: To test the obtained connection, test released connections: To test the released connection. Someone may ask, what is the purpose?
I don't know if you have encountered a Java connection failure exception in the project. I have encountered this exception only when the system is under heavy pressure. With this option, you don't have to worry about this problem-because the connection pool has already been tested for you, once you check that the connection is invalid, it will be discarded and returned to the database, only valid for you. However, most of the exceptions with this connection failure are caused by the application being less rigorous. We are more concerned about the code-but at least WebLogic wants to help you make up for it. Is it very considerate :)
Another important feature, of course, is connection pool monitoring. You can see the usage on the monitor tab. Someone has to ask again. There are no indicators. Don't forget the M view function link :)
The following indicators are available: Current connection count, once reached peak, available connections, number of pending connections, number of connections opened from the database, and number of connections that have been closed... The first three items are my most important items.

Usage evaluation: in specific project applications, the continuous operation stability of the connection pool is very strong, and the performance is quite good under the pressure of a large amount of concurrency, in addition, connections in the connection pool can be released in time in case of exceptions.
Connection pool monitoring is clear at a glance and timely available.

2. WebSphere connection pool
Let's start with another question: I remember someone said that Websphere is considered WebSphere only after version 6, and I personally agree with it. WebSphere 5 and earlier versions... Forget it.
In fact, the WebSphere connection pool adheres to the consistent style of IBM: Powerful, complex to use :)
Go to the console and use the "JDBC provider" function menu to perform basic configuration of the connection pool. All the way down, different database configuration methods are different. The most strange thing is that you need to manually add the user and Password parameters separately, if no
I am confused about the guidance of the materials. You can find many basic settings online. After the connection pool is set, you need to set the data source. The JNDI name is the same as the previous one: JDBC/MyApp
Advanced Settings include the number of initial connections, maximum connections, and connection validity check. No timeout is used .. In fact, these are roughly the same as the connection pool configuration in weblogic.
Connection pool monitoring: use the "run monitoring" menu. Select "JDBC monitoring" for a monitoring project. What's worse? What kind of graphical controls need to be installed in the server operating system, if you select Yes, you have to find the control installed in the operating system (Linux), and many dependent components do not exist... It took only half a day to find out whether or not the monitoring data and graphics were the same.
Despite some twists and turns, the monitoring content is still very powerful, the connection pool includes the current number of connections, the peaks that have been reached, the number of available connections, the number of connections opened from the database, and the number of connections that have been closed... Among them, the first three items are my most important. the strange phenomenon is that the number of connections enabled when the application was started did not reach the initial defined number of connections, it is unclear how Websphere is made on a computer.
In addition, when the pressure is high, the number of connections that can be used will be a negative number. At that time, it was strange that I thought it was okay. The negative number must be the number of waiting queues.

Usage evaluation: in specific project applications, the continuous operation stability of the connection pool is quite strong, and the performance is good enough under the pressure of a large amount of concurrency, in addition, connections in the connection pool can be released in time in case of exceptions,
The monitoring configuration of the connection pool is somewhat complicated, but after the configuration is complete, various metrics are displayed at a glance and graphic display is available.

Summary time:
I was deeply impressed by these two commercial-level connection pools, featuring powerful functions, stable use, excellent performance, and monitoring in place. It can be said that WebLogic connection pool configuration and monitoring configuration are simpler and clearer, while Websphere is more complex, but option functions are more. In fact, this is a direct reflection of the use of the two application servers. Of course, the general comparison of the two application servers should be another topic, maybe in the next issue...

Compared with the advantages and disadvantages of multiple connection pools, the following topic may not be directly related to the comparison, but I personally think it should be a more valuable experience to share, that is-such a multi-index configuration, what values should the maximum and minimum connections and other necessary configuration indicators be configured in a formal production project?
In fact, this value should first be determined based on the specific project situation, data size, and concurrency (although it seems like a joke, our R & D staff's rigorous style is still necessary :). Specifically, in a medium-sized or small-sized project, we recommend that you set WebLogic to the largest and minimum values of 300 for a number ranging from 3000 to 1 million, and the data size ranges from 0.1 billion to 200, webSphere has a minimum of 200 and a maximum of 300. The premise is that the minimum memory size set by 2 must be greater than 1 GB. Of course, if the condition permits a larger memory size, the better, however, the 32-Bit Memory is limited to GB (64-bit memory I am willing to set up a 4 GB memory, and the speed is very high ). I believe there will be a lot of problems when this number comes out. Let's talk about our experiences and ideas one by one.
1. Why is it 200 or 300 instead of higher?
A: It doesn't work much if the redistribution is too much. One is that the application server needs memory support to maintain the number of connections. I just mentioned that 32-bit machines can only support up to 1.5 GB, in addition, maintaining a large number of connections for allocation and usage is also a heavy load on the CPU, so it should not be too large.
2. Why not?
A: If it is too small, queuing will occur after the concurrency and data volume of the above-mentioned projects are increased, the system will slow down, the database connection will be frequently opened and closed, and there will be performance pressure, user experience is also poor.
3. Why does WebLogic have the same minimum and maximum sizes, while WebSphere does not?
A: In fact, it is the same as the minimum maximum value for memory allocation. Generally, we recommend that you keep the two values in the memory. But the two values officially recommended by IBM must be different-the official statement should be followed.
4 have you not mentioned other open source connection pool allocation schemes yet?
A: open-source individuals think that it will be enough to reach 100, and it will not be too stable if it is higher. Of course, the Minimum Memory of 1 GB must be allocated to Tomcat.
5. Are there other indicators?
A: Of course there are some other details, but I personally think the specific analysis is not listed here. If you are interested, please share it with me.

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.