java MySQL连接超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2594583/
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
MySQL connection timeout
提问by NikolayGS
I'm running program at apache tomcat server, that should be on permanently, but every morning(the client part isn't accessible at night) i receive error message (in apache tomcat console) that MySQL server is off. So is there any way to prevent this? Thanks in advance!
我在 apache tomcat 服务器上运行程序,它应该是永久的,但是每天早上(客户端部分在晚上无法访问)我收到错误消息(在 apache tomcat 控制台中)MySQL 服务器关闭。那么有什么办法可以防止这种情况发生吗?提前致谢!
回答by Maurice Perry
Yes, I have had the same problem. Try to add a validationQuery to your DataSource.
是的,我遇到了同样的问题。尝试向您的数据源添加一个validationQuery。
Here is how I have defined mine:
这是我如何定义我的:
<Resource auth="Container"
type="javax.sql.DataSource"
name="jdbc/gporder"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/mydb"
maxActive="10"
maxIdle="5"
validationQuery="SELECT 1"
testOnBorrow="true"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="10000"
minEvictableIdleTimeMillis="60000"
username="..." password="..."/>
回答by nos
the mysql server times out a connection after a while, by default it's 28800seconds, it's likely this timeout you're hitting.
mysql 服务器在一段时间后超时连接,默认情况下是28800秒,很可能是您遇到的超时。
You can just instruct the mysql driver to reconnect in case of a lost connection by adding the autoreconnect=trueparameter to the jdbc url, e.g. jdbc:mysql://localhost/mydb?autoreconnect=true
您可以通过将autoreconnect=true参数添加到 jdbc url来指示 mysql 驱动程序在连接丢失的情况下重新连接,例如jdbc:mysql://localhost/mydb?autoreconnect=true
回答by stacker
I suppose you open the connections directly in your web apps code. You could try to introduce a connection poolusing a timeout the connection should be reestablished.
我想您直接在您的网络应用程序代码中打开连接。您可以尝试使用超时来引入连接池,应该重新建立连接。
If this doesn't help, ask the network admin whether he has stateful filters which detect timeouts (while your client is idle) and close the connection.
如果这没有帮助,请询问网络管理员他是否有检测超时的状态过滤器(当您的客户端空闲时)并关闭连接。
If you're using a database pool, like c3p0 or dbcp, look in its documentation, there should be settings to configure idle timeouts or periodic checking of the connection (which will keep the connection open, and not time it out on the server side)
如果您使用的是数据库池,如 c3p0 或 dbcp,请查看其文档,应该有一些设置来配置空闲超时或定期检查连接(这将使连接保持打开状态,并且不会在服务器端超时)

