使用c3p0和直接使用jdbc连接的对比

  • Post author:
  • Post category:其他


DBPool是一个对连接池进行管理的类,然后使用的是c3p0。其中的main()方法测试了连接池和jdbc的连接数据库速度。

public class DBPool {

private static DBPool dbPool;

private ComboPooledDataSource dataSource;

static {


dbPool = new DBPool();

}

public DBPool() {


try {


dataSource = new ComboPooledDataSource();

dataSource.setUser(“root”);

dataSource.setPassword(“123456”);

dataSource

.setJdbcUrl(“jdbc:mysql://localhost:3306/jwdb”);

dataSource.setDriverClass(“com.mysql.jdbc.Driver”);

dataSource.setInitialPoolSize(2);

dataSource.setMinPoolSize(1);

dataSource.setMaxPoolSize(10);

dataSource.setMaxStatements(50);

dataSource.setMaxIdleTime(60);

} catch (PropertyVetoException e) {


throw new RuntimeException(e);

}

}

public final static DBPool getInstance() {


return dbPool;

}

public final Connection getConnection() {


try {


return dataSource.getConnection();

} catch (SQLException e) {


throw new RuntimeException(“无法从数据源获取连接 “, e);

}

}

public static void main(String[] args) throws SQLException {


//直接获取数据的测试,跟jdbc类似。


/*  Connection con = null;

String sql=”select agent_id from agent”;

PreparedStatement ps;

//List list=new ArrayList();

try {


con = DBPool.getInstance().getConnection();

ps=con.prepareStatement(sql);

ResultSet rs=ps.executeQuery();

while(rs.next()){


System.out.println(rs.getString(“agent_id”));

}

} catch (Exception e) {


} finally {


if (con != null)

con.close();

}

*/

System.out.println(“使用连接池…………………………..”);

for (int i = 0; i < 20; i++) {


long beginTime = System.currentTimeMillis();

Connection conn = DBPool.getInstance().getConnection();

try {


PreparedStatement pstmt = conn

.prepareStatement(“SELECT * FROM netbar”);

ResultSet rs = pstmt.executeQuery();

while (rs.next()) {


}

} 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();

try {


Class.forName(“com.mysql.jdbc.Driver”).newInstance();

} catch (Exception e1) {


e1.printStackTrace();

}

String url = “jdbc:mysql://localhost:3306/jwdb”;

String user = “root”;

String password = “123456”;

Connection conn = DriverManager.getConnection(url, user, password);

try {


PreparedStatement pstmt = conn

.prepareStatement(“SELECT * FROM netbar”);

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));

}

}

}

测试结果:

使用连接池…………………………..

第1次执行花费时间为:687

第2次执行花费时间为:16

第3次执行花费时间为:15

第4次执行花费时间为:16

第5次执行花费时间为:94

第6次执行花费时间为:0

第7次执行花费时间为:0

第8次执行花费时间为:16

第9次执行花费时间为:16

第10次执行花费时间为:15

第11次执行花费时间为:16

第12次执行花费时间为:15

第13次执行花费时间为:0

第14次执行花费时间为:16

第15次执行花费时间为:16

第16次执行花费时间为:15

第17次执行花费时间为:16

第18次执行花费时间为:16

第19次执行花费时间为:15

第20次执行花费时间为:0

不使用连接池…………………………..

第1次执行花费时间为:47

第2次执行花费时间为:31

第3次执行花费时间为:32

第4次执行花费时间为:15

第5次执行花费时间为:47

第6次执行花费时间为:16

第7次执行花费时间为:47

第8次执行花费时间为:15

第9次执行花费时间为:31

第10次执行花费时间为:32

第11次执行花费时间为:15

第12次执行花费时间为:32

第13次执行花费时间为:15

第14次执行花费时间为:32

第15次执行花费时间为:31

第16次执行花费时间为:15

第17次执行花费时间为:16

第18次执行花费时间为:31

第19次执行花费时间为:16

第20次执行花费时间为:16

以看出,在使用连接池时,第一次执行花费的时间稍长,因为第一次初始化操作需要创建多个连接并放入池中,以后使用时将会大大缩短执行时间。

在不使用连接池时,每次花费的时间都比较长。



版权声明:本文为iteye_7401原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。