HikariPool连接池的使用
2024-10-20 01:25:53
测试代码: 使用连接池用时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()实现
最新文章
- catalina
- NS2中trace文件分析
- php + Redis 写的类似于新浪微博的feed系统
- CentOS 6.3 NFS的安装配置、启动及mount挂载方法
- c# .NET 进行数据库备份和还原
- 简聊iOS支付集成(支付宝和微信支付)
- 配置zabbix agent向多个server发送数据
- yii动态配置International(Yii::t())
- mschart asp chart 用法,包括前台写法与后台写法,还有click事件,如何触发。
- MYSQL之IFNULL
- ex_BSGS
- pycharm5.0 快捷键大全osx
- Open Source Book For ML
- nginx反向代理 强制https请求
- vue ie
- Servlet封装类
- 同一个Tomcat部署两个project之间的通信问题
- sql中 in 、not in 、exists、not exists 用法和差别
- LintCode——颜色分类
- C++自学随笔
热门文章
- 【总结整理】dojo学习
- mt_rand()函数、str_shuffle() 函数、join() 函数
- php学习笔记-超级全局变量
- Spring第一篇
- 校对双层PDF中的隐藏文本
- 使用metasploit进行栈溢出攻击-5
- iOS中Info.plist文件的常见配置
- Git 分支管理-git stash 和git stash pop
- 【bzoj2434】: [Noi2011]阿狸的打字机 字符串-AC自动机-BIT
- 文章推荐一个Java程序员跟大家谈谈从业心得