Reprint: sorting out several mainstream Java connection pools

Pool technology can significantly optimize the performance of server applications, improve program execution efficiency and reduce system resource overhead to a certain extent. The pool mentioned here is a pool in a broad sense, such as database connection pool, thread pool, memory pool, object pool, etc. Among them, the object pool can be regarded as a container for saving objects, and a certain number of objects are created during process initialization. When necessary, a free object is directly taken from the pool. After it is used up, the object is not directly released, but put it into the object pool so that the next object request can be reused directly. The design idea of other pools is the same. The advantage of pool technology is that it can eliminate the delay caused by object creation, so as to improve the performance of the system.

To understand java connection pool, we must first understand the principle of database connection pool. Java connection pool is the application of database connection pool in Java. - we know that there is a famous design pattern for shared resources: resource pool.

This mode is to solve the problems caused by frequent allocation and release of resources. In order to solve the above problems, database connection pool technology can be used. The basic idea of database connection pool is to establish a "buffer pool" for database connections. Put a certain number of connections in the buffer pool in advance. When you need to establish a database connection, just take one from the buffer pool and put it back after use. We can prevent the system from connecting to the database endlessly by setting the maximum number of connections in the connection pool. More importantly, we can monitor the number and usage of database connections through the connection pool management mechanism, so as to provide a basis for system development, testing and performance adjustment.

C3p0 is an open source JDBC connection pool. It is published together with hibernate in the Lib directory, including the connection and statement pool datasources objects that implement the JDBC 3 and JDBC 2 extension specifications. (home page: http://sourceforge.net/projects/c3p0/ )

Bonecp is an open source fast JDBC connection pool. Bonecp is small, Only 40 K (log4j and Google collections are required to support the runtime, which add up to a lot). In contrast, c3p0 needs more than 600 K. in addition, I think bonecp has a disadvantage that the jdbc driver is loaded outside the connection pool, so it is not flexible enough in the configuration of some application servers. Of course, small size is not the reason for bonecp's excellence. What are bonecp's advantages What are the highlights? Please look at the performance test report. (home page: http://jol @R_ 665_ 2419@.com /)

DBCP (database connection pool) is a database connection pool that relies on the Jakarta commons pool object pool mechanism. The data source of Tomcat uses DBCP. At present, 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 the version, you should see what version of JDK you use Ben, there is no difference in function. (home page: http://commons.apache.org/dbcp/ )

Proxool is a Java SQL driver that provides connection pool encapsulation for other types of drivers you choose. It can be easily transplanted to existing code. Fully configurable. Fast, mature and robust. You can transparently add connection pooling to your existing jdbc driver.

Database connection is a key limited and expensive resource, which is particularly prominent in multi-user web applications. The management of database connection can significantly affect the scalability and robustness of the whole application and the performance index of the program. Database connection pool is proposed to solve this problem. Database connection pool is responsible for allocating, managing and releasing database connections. It allows applications to reuse an existing database connection instead of re establishing one; Release the database connection whose idle time exceeds the maximum idle time to avoid database connection omission caused by not releasing the database connection. This technology can significantly improve the performance of database operation.

Database connection pools commonly used 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

There are three common configurations of Hibernate connection pool: 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 explain several concepts of connection pool before introduction, which is helpful to understand some later words. The most original database use is to open a connection and use it. After use, you must close the connection to release resources. Due to frequent opening and closing connections, there is a certain resource load on the JVM, including the database. Especially when the application pressure is high, there is a lot of resource occupation, which is easy to cause performance problems. Therefore, the function of using the connection pool is obvious. In fact, its principle is not complex: first open a certain number of database connections, allocate them to the caller when using them, and return them to the connection pool after calling. Note that after returning to the connection pool, these connections will not be closed, but are ready to be allocated to the next caller. It can be seen that the connection pool saves a lot of actions of opening and closing database connections, which is self-evident for the improvement of system performance. Several concepts: minimum connection -- the number of connections opened immediately after the application is started and the subsequent minimum number of connections maintained. Maximum connections -- the maximum number of connections that an application can use and the number of connection growth -- the number of new connections that an application opens each time. Take an example to illustrate the operation of the connection pool: assuming that the minimum and maximum connections are set to 10 and 20, once the application is started, 10 database connections will be opened first, But note that the number of active connections in the database connection pool is 0 -- because you are not using these connections, and the number of idle connections is 10. Then you start to log in. Assuming that the login code uses a connection for query, the number of active connections in the database connection pool is 1 and the number of idle connections is 9. There is no need to open a connection from the database -- because the connection pool has prepared 10 for you. After login, what is the number of connections in the current connection pool? Of course, it is 0, because the connection has been returned to the connection pool with the end of the transaction. Then 11 people log in at the same time, What happens: the connection pool requests a new connection from the database (opens) a connection and sends it out together with another 10. At this moment, the number of connection pools used is 11, but it doesn't matter. Under normal circumstances, it will become 0 in a moment. What if 21 people log in at the same time? The 21st person can only release the connection to someone in front after logging in. At this time, the connection pool opens 20 database connections -- although it is likely to be running The number of connections used has been reduced to 0. Will 20 connections remain? Of course not. The connection pool will close a certain number of connections and return them to the database within a certain period of time. In this example, the number is 20-10 = 10, because only the minimum number of connections needs to be maintained, and this time period is also configured in the connection pool. Well, the basic concepts are finished. Let's get down to business and make a comparison. First of all, it is recommended to use JNDI to uniformly configure the connection pool in order to facilitate migration and configuration. In fact, there are many examples on the Internet, Here is a simple example (using the spring framework): first, configure the JNDI name in the context definition of the application, such as a resource.xml file, which is written as JDBC / myapp. Note that the bean datasource needs to be configured into all beans as the attribute of datasource name in the configuration file of Dao layer (hibernate or JDBC), in which "JDBC / MYDS" is the JNDI name, The next step is to connect the database in the application server connection pool and configure an open source data connection pool for the corresponding JNDI. 1 dbcpdbcp may be the most used open source connection pool, probably because it is easy to configure, and many open source and Tomcat application examples use this connection pool. This connection pool can set the maximum and minimum connections, connection waiting time and other basic functions. For the configuration of this connection pool, see: DBCP XML usage evaluation: in specific project applications, it is found that the continuous operation stability of this connection pool is OK, but the speed is slightly slow, and the stability decreases under the pressure of large concurrency. In addition, it does not provide connection pool monitoring. 2 c3p0c3p0 is another open source connection pool, which is also famous in the industry. This connection pool can set the maximum and minimum connections, Connection waiting time and other basic functions. For the configuration of this connection pool, see: c3p0.0 in the attachment package xml。 Usage evaluation: in the specific project application, it is found that the continuous operation stability of this connection pool is quite good, and the stability is guaranteed under the pressure of large concurrency. In addition, connection pool monitoring is not provided. 3 Proxool Proxool this connection pool may be used by few people, but it also has a certain popularity. This connection pool can set the maximum and minimum connections, connection waiting time and other basic functions. For the configuration of this connection pool, see Proxool. Com in the attachment package xml。 Usage evaluation: in the specific project application, it is found that there are some problems with the continuous operation stability of this connection pool. There is a task scenario task that needs to run batches for a long time. The same code ends successfully in the other two open source connection pools, but exits abnormally in Proxool. But Proxool has one advantage -- connection pool monitoring, which is a very attractive thing. The general configuration method is on the web Add the following definition to XML: admin.org logicalcobwebs. proxool. admin. servlet. Adminservlet admin / Admin and access after the application starts: http://localhost:8080/myapp/admin This URL can be monitored, but the package of Proxool itself will have coding problems during monitoring. There is a package to solve this problem in the attachment. See proxool-0.9 in the attachment compressed package 0RC3. jar。 In addition, jdk1 Environment above 5. Summary time: To sum up, these open source connection pools have their own advantages and disadvantages. It is recommended to use c3p0. After inspection, this connection pool has stable performance and strong pressure bearing capacity. Although Proxool has obvious performance problems, it is recommended to use it during development and testing because it has monitoring function, which helps to determine whether any connection has not been turned off and eliminate the performance problems of some codes. 2. Commercial middleware connection pools. Several open source connection pools are listed above. In fact, it is certain that if conditions permit the use of middleware such as Weblogic and WebSphere, don't hesitate to use the database connection pools provided by these commercial middleware. Their performance, deployment and open source are not in the same order of magnitude. For example, There was a project with a large amount of data. For the same code application, system exceptions occurred in all three open source connection pools, while the connection pools of Weblogic and WebSphere operated normally. Of course, it was later found that the code had some defects, but it also showed the strength of the commercial connection pool. 1 Weblogic's connection pool Weblogic 8 is a very easy and convenient application server software, but it's torture when it comes to 9. I don't know what bea thinks. After Oracle acquired BEA, it made 10, which is much better than 9, but its favorite is 8... No more digressions, Let's introduce his database connection pool in version 8.1 (in fact, the configuration of 10 is similar). First, the basic settings of the connection pool. There are many tutorials on the Internet. Then, enter the data sources menu to configure the JNDI name in the data source, which should be consistent with the previous application configuration: JDBC / myapp. Then, the configuration of some specific items of the connection pool, including setting the minimum (initial capacity) and the maximum (maximum capacity) connections, and how many capacity increases need to be added at one time each time the 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 example described in the previous connection pool). In addition, there are several interesting options: Test reserved connections: test the obtained connections, and test released connections: test the released connections. Some people will ask, what's the use of this? I don't know Do you encounter any exception of Java report connection failure in the project? Anyway, I have encountered it. It only appears when the system pressure is high. With this option, you don't have to worry about this problem - because the connection pool has been tested for you. Once it is checked that the connection is invalid, it will be discarded and returned to the database, and only valid ones will be given to you. However, this connection failure exception is mostly caused by the lax application. We should be more concerned about the code -- but at least Weblogic wants to help you make up for it. Isn't it very considerate:) of course, another important function is connection pool monitoring: the usage can be seen in the monitor tab. Someone will ask again. There are no indicators, Don't forget the function link of custom view:) there are the following indicators: the number of current connections, the peak value reached, the number of connections available, the number of connections waiting, the number of connections opened from the database, the number of connections closed... Among them, the first three are my most concerned use evaluations: in specific project applications, the continuous operation stability of this connection pool is very strong, and its performance is quite excellent under the pressure of large concurrency. In addition, the connections in the connection pool can be released in time under some abnormal conditions. Connection pool monitoring is clear at a glance and in place in time. 2. The connection pool of WebSphere is a digression: I remember someone said that WebSphere can only be regarded as WebSphere after version 6, and I personally agree with it. WebSphere 5 and earlier... Forget it. In fact, WebSphere's connection pool adheres to IBM's consistent style: powerful and complex:) enter the console and use the "JDBC provider" function menu to configure the basic connection pool. Along the way, different database configuration methods are different. The strangest thing is to manually add the user and password parameters separately, If there is no information guidance, I really can't figure it out. These basic settings are still available online. After setting the connection pool, you also need to set the data source. The JNDI name is the same as the previous correspondence: JDBC / myapp advanced settings include initialization of connections, maximum connections, connection validity check, and no timeout.. 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 selection of monitoring items. Just select JDBC monitoring. What's hateful is that what server operating system needs to be installed and what graphical controls pop up at the beginning, Select Yes, then you have to find the control in the operating system (Linux) installation, and then many dependent components are not available... It took a long time to find out whether to choose or not, and the monitoring data and graphics can come out as well. It's really angry. Although after some twists and turns, the monitoring content is still very powerful. As for the connection pool, it includes the current number of connections, the peak value reached, the number of connections that can be used, and opening from the database Number of connections, number of connections that have been closed... Among them, the first three items are my most concerned. The strange phenomenon is that the number of opened connections when the application is just started does not reach the number of initially defined connections. I don't know how WebSphere is a computer system. In addition, when the pressure is high, the number of connections that can be used will be negative. It's strange at that time. It's clear that the negative number must be the number of queued connections. Usage evaluation: in specific project applications, the continuous operation of this connection pool is very important

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>