Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of mysql5 database connection timeout

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to solve the problem of mysql5 database connection timeout", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the problem of mysql5 database connection timeout.

The solution to the mysql5 database connection timeout problem recently encountered a problem with an mysql5 database. Is a standard servlet/tomcat network application, using mysql database in the background. The problem is that after a night of standby, the first login the next morning always fails. Check the log and find the following error: "com.mysql.jdbc.exceptions.jdbc4.Communication***ception:

Communications link failure Last packet sent to the server was 0 ms ago. "after some research, it is found that many people have encountered similar problems, but there are not many satisfactory answers online. There are also a lot of questions on mysql, but there are no correct answers; Baidu knows that there is a nearly correct answer. Now I will summarize my solution: the above problems are caused by the configuration of the mysql5 database. Mysql5 defaults the wait time (wait_timeout) for its connection to 8 hours. You can view its value in its client program like this: mysql >

Mysql > show global variables like 'wait_timeout' +-+-+ | Variable_name | Value | +-+-+ | wait_timeout | 28800 | +-+-+ 1 row in set (28800 sec) 28800 seconds, that is, 8 hours. If the database connection (java.sql.Connection) has been waiting for a period of wait_timeout seconds, mysql5 closes the connection. At this point, your Java application's connection pool still legally holds a reference to the connection. The above error is encountered when the connection is used for database operations. This explains why my program can't log in the next day. You may wonder if there is a solution in the data source configuration of tomcat. Indeed, you can attach "autoReconnect=true" to the configuration where jdbc connects to url, but this only works for previous versions of mysql5. Adding "validation query" does not seem to help. I think the easiest way is to prescribe the right medicine: since the problem is caused by the small default value of mysql5's global variable wait_timeout, let's just make it bigger. Looking at the mysql5 manual, it is found that the maximum value for wait_timeout is 24 days / 365 days (windows/linux). Take windows as an example, suppose we want to set it to 21 days, we just need to modify the mysql5 configuration file "my.ini" (mysql5 installation dir) and add a line: wait_timeout=1814400 needs to restart mysql5. Linux system configuration file: / etc/my.cnf tests show that the problem is resolved.

At this point, I believe you have a deeper understanding of "how to solve the problem of mysql5 database connection timeout". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report