测试代码: 使用连接池用时500ms, 不使用连接池用时8s, 相差16倍. 
每个连接的创建都要经过http握手、密码认证,这里比较耗时, mysql给每个connection分配一个id
public class DBpoolTest {

    private static final HikariDataSource ds;

    static {
HikariConfig conf = new HikariConfig();
conf.setUsername("root");
conf.setPassword("root");
conf.setJdbcUrl("jdbc:mysql://localhost:3306/abc");
ds = new HikariDataSource(conf);
} @Test
public void test_1() throws SQLException {
long st = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Connection connection = ds.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select now() from dual");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
connection.close();
}
System.out.println(System.currentTimeMillis() - st);
} @Test
public void test_2() throws SQLException {
long st = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc", "root", "root");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select now() from dual");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
connection.close();
}
System.out.println(System.currentTimeMillis() - st);
}
}

  

连接池回收过程

HikariDataSource datasource= new HikariDataSource( xxxx );

Connection cn = datasource.getConnection();

try { cn.doXXX()
} finnally(){

     connection.close();// connection的实现类(代理类)自己会调用hikariPool.evictConnection(cn) ,将此连接回收到pool中
}

connection回收到池中是连接池自己做的事, 业务代码不用关心物理连接是否真的关闭,只需要调用close()方法


Hikari中connection.close()的实现 
 

Druid close()实现



最新文章

  1. catalina
  2. NS2中trace文件分析
  3. php + Redis 写的类似于新浪微博的feed系统
  4. CentOS 6.3 NFS的安装配置、启动及mount挂载方法
  5. c# .NET 进行数据库备份和还原
  6. 简聊iOS支付集成(支付宝和微信支付)
  7. 配置zabbix agent向多个server发送数据
  8. yii动态配置International(Yii::t())
  9. mschart asp chart 用法,包括前台写法与后台写法,还有click事件,如何触发。
  10. MYSQL之IFNULL
  11. ex_BSGS
  12. pycharm5.0 快捷键大全osx
  13. Open Source Book For ML
  14. nginx反向代理 强制https请求
  15. vue ie
  16. Servlet封装类
  17. 同一个Tomcat部署两个project之间的通信问题
  18. sql中 in 、not in 、exists、not exists 用法和差别
  19. LintCode——颜色分类
  20. C++自学随笔

热门文章

  1. 【总结整理】dojo学习
  2. mt_rand()函数、str_shuffle() 函数、join() 函数
  3. php学习笔记-超级全局变量
  4. Spring第一篇
  5. 校对双层PDF中的隐藏文本
  6. 使用metasploit进行栈溢出攻击-5
  7. iOS中Info.plist文件的常见配置
  8. Git 分支管理-git stash 和git stash pop
  9. 【bzoj2434】: [Noi2011]阿狸的打字机 字符串-AC自动机-BIT
  10. 文章推荐一个Java程序员跟大家谈谈从业心得