java - jdbc connectTimeout 与 jdbc loginTimeout

标签 java mysql postgresql jdbc timeout

我们的项目要求支持 Postgres(Postgresql 驱动程序)的“jdbc 超时”功能。 我们还支持 Microsoft SQL(JTDS 驱动程序)和 MySQl(mysql 驱动程序)。所以我想介绍“loginTimeout”作为所有数据库的共同特征。

在查看驱动程序文档时,我发现 JTDS 和 Postgresql 驱动程序都支持一个名为“loginTimeout”的 jdbc 参数,但 Msql 不支持

http://jtds.sourceforge.net/faq.html

loginTimeout (default - 0 for TCP/IP connections or 20 for named pipe connections) The amount of time to wait (in seconds) for a successful connection before timing out. If a TCP/IP connection is used to connect to the database and Java 1.4 or newer is being used, the loginTimeout parameter is used to set the initial connection timeout when initially opening a new socket. A value of zero (the default) causes the connection to wait indefinitely, e.g.,until a connection is established or an error occurs. See also socketTimeout. If a named pipe connection is used (namedPipe is true) and loginTimeout is greater than zero, the value of loginTimeout is used for the length of the retry period when "All pipe instances are busy" error messages are received while attempting to connect to the server. If loginTimeout is zero (the default), a value of 20 seconds is used for the named pipe retry period.

http://jdbc.postgresql.org/documentation/84/connect.html

loginTimeout = int Specify how long to wait for establishment of a database connection. The timeout is specified in seconds.

但对于 Mysql 来说没有类似 loginTimeout 的东西,但是有

connectTimeout: Timeout for socket connect (in milliseconds), with 0 being no timeout. Only works on JDK-1.4 or newer. Defaults to '0'

所以我的问题是“connectTimeout 和 loginTimeout 之间有什么区别”,它们是否具有相同的功能?

最佳答案

MySQL 的 socketConnect 设置决定客户端尝试打开网络连接的时间;它没有声称数据库本身将进行身份验证或运行,只是可以建立一个套接字。

相比之下,Postgres 表示连接到数据库的最长时间;如果您的命令在超时之前返回,则数据库收到网络请求并在适当的时间内做出响应。

所以在一种情况下,您只是对网络行为施加了限制(即您等待将套接字连接到该端口上的该服务器的时间);另一方面,您对数据库行为施加了限制(即等待数据库本身连接的时间)。

作为旁注,javax.sql.CommonDataSource 接口(interface)为 getLoginTimeout 规定了一个 JDBC 属性,它模仿了 PostgreSQL 属性的行为。当您查看 MysqlDataSource(例如 mariadb)的各种实现时,这些方法并未实现;这通常让我相信在 MySQL 中没有直接的类似物。

关于java - jdbc connectTimeout 与 jdbc loginTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26250141/

相关文章:

java - 每次更改我的 Java 代码时,我可以阻止重新启动我的应用程序吗?

java - 有关 JSR-303、Tapestry 和 JPA 实体继承的复杂用例

java - 使用 Java Bean Validation 验证 String 数组的元素

mysql - 无法安装 mysql 社区服务器 8.0 windows 10。我已经安装了 Server 5.7 和 Workbench 8.0 CE

mysql - 从 CSV 导入时 PHPMYADMIN 权限被拒绝

java - JCalendar 多日选择

python - 插入mysql数据库时间戳

Django 应用程序在 Travis CI : server not running on localhost 中失败

python - 通过 PostgreSQL 触发器记录当前 Python 解释器的 Stacktrace

java - 每 30 秒查询 PostgreSQL 的有效方法?