时间:2021-07-01 10:21:17 帮助过:28人阅读
作为开源的数据库连接池,C3P0是一个优秀的连接池,性能也十分可靠。
package com.zww.server;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public final class ConnectionManager {
//使用单利模式创建数据库连接池
private static ConnectionManager instance;
private static ComboPooledDataSource dataSource;
private ConnectionManager() throws SQLException, PropertyVetoException {
dataSource = new ComboPooledDataSource();
dataSource.setUser("root"); //用户名
dataSource.setPassword("123456"); //密码
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/zww");//数据库地址
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setInitialPoolSize(5); //初始化连接数
dataSource.setMinPoolSize(1);//最小连接数
dataSource.setMaxPoolSize(10);//最大连接数
dataSource.setMaxStatements(50);//最长等待时间
dataSource.setMaxIdleTime(60);//最大空闲时间,单位毫秒
}
public static final ConnectionManager getInstance() {
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;
}
}
下面是测试代码:
package com.zww.server;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
public class ConnectionDemo {
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();
try {
PreparedStatement pstmt = conn.prepareStatement("select * from event");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
// do nothing...
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime));
}
System.out.println("不使用连接池................................");
for (int i = 0; i < 20; i++) {
long beginTime = System.currentTimeMillis();
MysqlDataSource mds = new MysqlDataSource();
mds.setURL("jdbc:mysql://localhost:3306/zww");
mds.setUser("root");
mds.setPassword("123456");
Connection conn = mds.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement("select * from event");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
// do nothing...
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("第" + (i + 1) + "次执行花费时间为:"
+ (endTime - beginTime));
}
}
测试结果表明,在使用连接池时,只在第一次初始化时,比较耗时,完成初始化之后,使用连接池进行数据库操作明显比不使用连接池花费的时间少。
版权声明:本文为博主原创文章,未经博主允许不得转载。
数据库连接池的理解和使用
标签:数据库连接池 数据库 c3p0