简体   繁体   English

MySQL连接超时

[英]MySQL connection timeout

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. 我正在apache tomcat服务器上运行程序,应该是永久性的,但每天早上(客户端部分在晚上无法访问)我收到MySQL服务器关闭的错误消息(在apache tomcat控制台中)。 So is there any way to prevent this? 那有什么方法可以防止这种情况发生吗? Thanks in advance! 提前致谢!

Yes, I have had the same problem. 是的,我遇到了同样的问题。 Try to add a validationQuery to your DataSource. 尝试将validationQuery添加到DataSource。

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="..."/>

the mysql server times out a connection after a while, by default it's 28800 seconds, 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=true parameter to the jdbc url, eg jdbc:mysql://localhost/mydb?autoreconnect=true 你可以通过在jdbc url中添加autoreconnect = true参数来指示mysql驱动程序在连接丢失的情况下重新连接,例如jdbc:mysql://localhost/mydb?autoreconnect=true

I suppose you open the connections directly in your web apps code. 我想你直接在你的网络应用程序代码中打开连接。 You could try to introduce a connection pool using 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,请查看其文档,应该有设置来配置空闲超时或定期检查连接(这将保持连接打开,而不是在服务器端计时) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM