java MySql 连接器/J 中的 autoReconnect 和 autoReconnectForPools 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/527061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is difference between autoReconnect & autoReconnectForPools in MySql connector/J?
提问by ashitaka
In the configuration reference for MySql's connector J driver, a caveat emptor is issued on the use of the autoReconnectproperty. I followed the instructions and increased my server's wait_timeout. Since I am using DBCP (I am considering moving to c3po after reading several posts on Stackoverflow shooting down DBCP ), is it ok to use the autoReconnectForPoolsproperty ? What does it actually do when enabled under DBCP or any connection pool for that matter ?
在 MySql 的连接器 J 驱动程序的配置参考中,对autoReconnect属性的使用发出了警告。我按照说明操作并增加了服务器的wait_timeout。由于我正在使用 DBCP(在阅读有关 Stackoverflow 击落 DBCP 的几篇文章后,我正在考虑转向 c3po),是否可以使用autoReconnectForPools属性?在 DBCP 或任何连接池下启用时,它实际上会做什么?
采纳答案by David Rabinowitz
autoReconnect will throw an SQLException to the client, but will try to re-establish the connection.
autoReconnect 将向客户端抛出 SQLException,但会尝试重新建立连接。
autoReconnectForPools will try to ping the server before each SQL execution.
autoReconnectForPools 将在每次执行 SQL 之前尝试 ping 服务器。
I had a lot of issues with dbcp in the past, especially disconnections. Most were solved by moving to c3p0. Notice that the mysql driver has connection tester for c3p0 (com.mysql.jdbc.integration.c3p0.MysqlConnectionTester).
过去,我在使用 dbcp 时遇到了很多问题,尤其是断开连接。大多数都通过移动到c3p0来解决。请注意,mysql 驱动程序具有 c3p0 的连接测试器(com.mysql.jdbc.integration.c3p0.MysqlConnectionTester)。
Also, you may want to check this out: Connection pooling options with JDBC: DBCP vs C3P0
此外,您可能想看看这个:使用 JDBC 的连接池选项:DBCP 与 C3P0
回答by rustyx
MySQL's autoReconnectfeature is deprecated, as it has many issues (ref: official documentation).
MySQL 的autoReconnect功能已被弃用,因为它有很多问题(参考:官方文档)。
autoReconnectForPoolshas little to do with autoReconnect, it has more to do with autoCommitand reconnectAtTxEnd- when all 3 are true, it will ping the server at the end of each transaction and automatically reconnect if needed.
autoReconnectForPools与 关系不大autoReconnect,更多的是与autoCommit和有关reconnectAtTxEnd- 当所有 3 个都为 时true,它将在每个事务结束时 ping 服务器并在需要时自动重新连接。
DBCP's connection validation is imperfect - even when testOnBorrowis set, it sometimes returns broken connections from the pool (not to mention testing a connection before every borrow is horribly inefficient).
DBCP 的连接验证是不完善的 - 即使testOnBorrow设置了,它有时也会从池中返回断开的连接(更不用说在每次借用之前测试连接的效率都非常低)。
According to this article, HikariCP seems to be a better pool implementation, as it is able to use JDBC4 isValid()API which is much faster than running a test query, and is specially designed to never return broken connections to the client application.
根据这篇文章,HikariCP 似乎是一个更好的池实现,因为它能够使用isValid()比运行测试查询快得多的JDBC4 API,并且专门设计为永远不会向客户端应用程序返回断开的连接。
回答by StatsTrade
Are you sure you're using DBCP properly?
您确定正确使用 DBCP 吗?
According to the short configuration notes, it's supposed to handle timeouts pretty well thanks to the default value of testOnBorrow=true(tests the connection before used, and if it fails it is dropped from the pool and we try to get a new one instead).
根据简短的配置说明,由于默认值testOnBorrow=true(在使用前测试连接,如果连接失败,它将从池中删除,我们尝试获取一个新连接) ,它应该可以很好地处理超时。
The only thing you need to dois to make sure you configure the validationQueryproperty to a non-null String, e.g. "SELECT 0" for MySQL database (hereis a post about different validationQuery values per DB used).
您唯一需要做的就是确保将validationQuery属性配置为非空字符串,例如 MySQL 数据库的“SELECT 0”(这是一篇关于每个数据库使用不同验证查询值的文章)。

