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.
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.
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.
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.
1 Comparison of c3p0, DBCP, proxool, and bonecp
1.1 test environment:
Operating System: Windows XP SP3
Database: MySQL 5.1
1.2 test conditions:
Initialsize = 30;
Maxsize = 200;
Minsize = 30;
Other parameters are default values;
1.3 Test code:
Use Java code to simulate multithreading to test the three database connection pools and determine the efficiency through the time spent
Dbtest. Java:
Public class dbtest implements runnable/* extends thread */{
Public long date1 = 0;
Private Static proxool;
Public static int COUNT = 0;
Public static void main (string [] ARGs) throws exception {
// DBCP. INIT ();
// C3p0. INIT ();
// Proxool = proxool. getinstance ();
Bonecpconn. INIT ();
Dbtest test = new dbtest ();
Test. startup ();
}
Public void startup (){
For (INT I = 0; I <5; I ++ ){
Thread thread = new thread (this );
Thread. Start ();
}
}
Public void run (){
If (date1 = 0)
{
Date1 = system. currenttimemillis ();
System. Out. println (date1 );
}
For (INT I = 0; I <10; I ++ ){
Try {
// Connection conn = DBCP. getconnection ();
// Connection conn = c3p0. getconnection ();
// Connection conn = proxool. getconnection ();
Connection conn = bonecpconn. getconnection ();
If (Conn! = NULL ){
Statement statement = conn. createstatement ();
Resultset rs = statement.exe cutequery ("select * from user where id = 1 ");
While (Rs. Next ()){
String username = Rs. getstring (2 );
System. Out. println (username );
}
Rs. Close ();
Statement. Close ();
Conn. Close ();
}
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
Count ++;
If (COUNT = 5)
{
Long date2 = system. currenttimemillis ();
System. Out. println (date2 );
System. Out. println ("finished! Time consumed: "+ (date2-date1) +" Ms ");
}
}
}
4.3.1 simulate 10 concurrent accesses to the database by five threads in a loop
DBCP takes 1181 Ms
860 ms for c3p0
Proxool 1563 Ms
Bonecp takes 31 Ms
4.3.2 simulate 10 threads for 10 concurrent accesses to the database
DBCP takes 1188 Ms
953 ms for c3p0
Proxool 1625 Ms
Bonecp Duration: 63 MS
4.3.3 simulate 20 threads for 10 concurrent accesses to the database
DBCP takes 1204 Ms
1000 ms for c3p0
Proxool 1640 Ms
Bonecp takes 110 ms
4.3.4 simulate 30 threads for 10 concurrent accesses to the database
DBCP takes 1250 Ms
1047 ms for c3p0
Proxool 1657 Ms
Bonecp takes 156 Ms
4.3.5 simulate 10 concurrent accesses to the database with 50 threads in a loop
DBCP takes 1406 Ms
1343 ms for c3p0
Proxool 1843 Ms
Bonecp takes 172 Ms
4.3.6 simulate 10 concurrent accesses to the database with 100 threads in a loop
DBCP takes 1641 Ms
2703 ms for c3p0
Proxool 2031 Ms
Bonecp takes 532 Ms
4.3.7 simulate 10 concurrent accesses to the database with 200 threads in a loop
DBCP takes 2093 Ms
4891 ms for c3p0
Proxool 2406 Ms
Bonecp takes 936 Ms
4.3.8 simulate 10 concurrent accesses to the database with 500 threads in a loop
DBCP takes 3219 Ms
11703 ms for c3p0
Proxool 3343 Ms
Bonecp takes 1922 Ms
4.3.9 simulate 10 concurrent accesses to the database with 800 threads in a loop
DBCP takes 4688 Ms
12063 ms for c3p0
Proxool 4141 Ms
Bonecp takes 2859 Ms
4.3.10 simulate 10 concurrent accesses to the database with 1000 threads in a loop
DBCP takes 5187 Ms
12563 ms for c3p0
Proxool 4703 Ms
Bonecp takes 3610 Ms
4.3.11 simulate 10 concurrent accesses to the database with 3000 threads in a loop
DBCP takes 14094 Ms
16297 ms for c3p0
Proxool 11344 Ms
Bonecp takes 11391 Ms
4.3.12 simulate 10 concurrent accesses to the database with 5000 threads in a loop
DBCP takes 23610 Ms
22032 ms for c3p0
Proxool 20125 Ms
Bonecp takes 17125 Ms
4.4 Test Result Analysis:
Bonecp always maintains the best performance
4.5 Test conclusion
Performance tests on the four database connection pools show that bonecp has better performance than the other three.
Q &
Q: Does J2EE use the struts connection pool or Tomcat connection pool?
A: The Tomcat connection pool is actually DBCP .. Tomcat and struts are not well written. The configuration connection pool in Tomcat is just an immature project. It is best to use it only for learning and small projects. Struts is a presentation layer framework and is not suitable for processing underlying data management.
Q: What about spring connection pool?
A: No matter what persistence technology is used, you must access the database through a data connection. In spring, data connections are obtained through data sources. In the past, data sources were generally provided by web application servers (such as Tomcat. In spring, you can not only obtain the data source of the application server through JNDI, but also directly configure the data source in the spring container. In addition, you can create a data source using code to perform a dependency-free unit test. Spring contains Implementation Packages for two data sources in the third-party dependency package. One is Apache DBCP and the other is C3PO. You can use either of the two configuration data sources in the configuration file. Spring also provides a simple data source implementation class org. springframework. JDBC. datasource. drivermanagerdatasource,
It implements the javax. SQL. datasource interface, but does not provide a pooled Connection Mechanism. Every time getconnection () is called to obtain a new connection, it simply creates a new connection. Therefore, this data source class is suitable for unit testing or simple independent applications because it does not require additional dependent classes.