之前轉載了一個關於連接池的博客:http://blog.csdn.net/qq407388356/article/details/78869653介紹了連接池的原理。後來在Spring整合的項目中使用到了連接池的一些操作。
下面以c3p0連接池與不使用連接池進行一個比較操作,使用maven比較簡單獲得c3p0的jar包。
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
下面簡單demo需要導入相關jar包:c3p0-0.9.1.1.jar。當然還需要mysql驅動的jar包mysql-connector-java-5.1.7-bin.jar。
ConnectionManager.java
c3p0連接池的Manager,通過ConnectionManager.getInstance().getConnection();獲得數據庫的Connection。
package test_c3p0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
public class ConnectionManager {
//使用單例模式創建數據庫連接池
private static ConnectionManager instance;
private static ComboPooledDataSource dataSource;
private ConnectionManager() throws PropertyVetoException{
dataSource = new ComboPooledDataSource();
dataSource.setUser("root"); //用戶名
dataSource.setPassword("root"); //密碼
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test");//數據庫地址
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setInitialPoolSize(5); //初始化連接數
dataSource.setMinPoolSize(1);//最小連接數
dataSource.setMaxPoolSize(10);//最大連接數
dataSource.setMaxStatements(50);//最長等待時間
dataSource.setMaxIdleTime(60);//最大空閒時間,單位毫秒
}
public static final ConnectionManagergetInstance() {
if (instance == null) {
try {
instance = new ConnectionManager();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}
public synchronized final Connection getConnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
Main.java
測試函數,包括使用連接池和不使用連接池的情況。
import test_c3p0.ConnectionManager; import java.sql.*; public class Main { public static void main(String[] args) throws SQLException { System.out.println("使用連接池................................"); for (int i = 0; i < 20; i++) { long beginTime = System.currentTimeMillis(); Connection conn = ConnectionManager.getInstance().getConnection(); query(conn); long endTime = System.currentTimeMillis(); System.out.println("第" + (i + 1) + "次執行花費時間爲:" + (endTime - beginTime)); } System.out.println("不使用連接池................................"); final String URL = "jdbc:mysql://localhost:3306/test"; final String USER = "root"; final String PASSWORD = "root"; for (int i = 0; i < 20; i++) { long beginTime = System.currentTimeMillis(); Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); query(conn); long endTime = System.currentTimeMillis(); System.out.println("第" + (i + 1) + "次執行花費時間爲:" + (endTime - beginTime)); } } public static void query(Connection conn) { try { PreparedStatement pstmt = conn.prepareStatement("select * from test"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { // do nothing... } } catch (SQLException e) { e.printStackTrace(); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
輸出:
使用連接池................................
二月 25, 20183:08:59 下午com.mchange.v2.log.MLog <clinit>
信息: MLogclients using java 1.4+ standard logging.
二月 25, 20183:08:59 下午com.mchange.v2.c3p0.C3P0Registry banner
信息:Initializing c3p0-0.9.1.1 [built 15-March-2007 01:32:31; debug? true; trace:10]
二月 25, 20183:09:00 下午com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息:Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource (連接池初始化信息…)
第1次執行花費時間爲:1619
第2次執行花費時間爲:2
第3次執行花費時間爲:2
…
第19次執行花費時間爲:3
第20次執行花費時間爲:1
不使用連接池................................
第1次執行花費時間爲:28
第2次執行花費時間爲:26
…
第19次執行花費時間爲:19
第20次執行花費時間爲:72
明顯可以看出使用連接池時,第一次初始化時間比較長,後面獲得連接時間很短。而不使用連接池每次都會有一定的耗時(比較平均)。在Javaweb一些應用中,面對海量的查詢操作必然需要使用連接池的。