gpt4 book ai didi

mysql - 使用 HikariCP 和 Slick 管理 MySQL 连接

转载 作者:行者123 更新时间:2023-11-29 07:50:10 25 4
gpt4 key购买 nike

我正在该软件堆栈上运行 Scala 应用程序:

Mac OS X 10.6.8 Snow Leopard
MySQL 5.1.46
Java 1.6.0_65
Scala 2.11.2
ConnectorJ 5.1.33
HikariCP 2.1.0
Slick 2.1.0

我不明白为什么即使在关闭 Scala 应用程序后,与 MySQL 的打开连接仍保持打开状态。唯一正确的方面是 Threads_connected 从 16 下降到 1(这是我执行“show status”命令的控制台。

mysql> show status like '%onn%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| Aborted_connects | 0 |
| Connections | 77 |
| Max_used_connections | 19 |
| Ssl_client_connects | 0 |
| Ssl_connect_renegotiates | 0 |
| Ssl_finished_connects | 0 |
| Threads_connected | 1 |
+--------------------------+-------+
7 rows in set (0.00 sec)

奇怪的是,每次运行应用程序时,我总是看到数据库的打开连接数随着连接池中设置的最大打开连接数(HikariCP maxPoolSize)的增长而增长,因此我可以声明连接永远不会返回到连接池以供重用。

根据 Slick 文档使用

db withSession { implicit session => 
/* entering the scope, getting 1 conn from the pool */
/*
do something within the session using the connection I've gotten
*/
}
/* here I'm out of the 'withSession' scope, and the
connection should be released */

进入其范围时将从池中获取连接,并在超出范围时将其释放

我是否做错了什么,或者我在该软件堆栈上的连接池使用方面出现了问题?

最佳答案

Connections 是自上次启动 mysqld 以来已进行的连接尝试次数的计数器。该计数器始终增加;当连接结束时它不会减少。

该计数器不是当前连接数,而是 Threads_connected

关于mysql - 使用 HikariCP 和 Slick 管理 MySQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26691092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com