MySQL connection after a period of inactivity after an unexpected solution

Source: Internet
Author: User

  • The most recently made Web site uses a MySQL database to discover that the application does not access the database for more than 8 hours, and the database disconnects. A second visit throws an exception as follows:

    Com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link Failurelast Packet sent to the server was 0  Ms ago. At Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (nativeconstructoraccessorimpl.java:57) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (delegatingconstructoraccessorimpl.java:45) at Java.lang.reflect.Constructor.newInstance (constructor.java:525) at Com.mysql.jdbc.Util.handleNewInstance ( util.java:406) at Com.mysql.jdbc.SQLError.createCommunicationsException (sqlerror.java:1074) at Com.mysql.jdbc.ConnectionImpl.createNewIO (connectionimpl.java:2120) at Com.mysql.jdbc.connectionimpl.<init > (connectionimpl.java:723) at com.mysql.jdbc.jdbc4connection.<init> (jdbc4connection.java:46) at Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (nativeconstructoraccessorimpl.java:57) atSun.reflect.DelegatingConstructorAccessorImpl.newInstance (delegatingconstructoraccessorimpl.java:45) at Java.lang.reflect.Constructor.newInstance (constructor.java:525) at Com.mysql.jdbc.Util.handleNewInstance ( util.java:406) at Com.mysql.jdbc.ConnectionImpl.getInstance (connectionimpl.java:302) at Com.mysql.jdbc.NonRegisteringDriver.connect (nonregisteringdriver.java:282) at Java.sql.DriverManager.getConnection (drivermanager.java:579) at Java.sql.DriverManager.getConnection ( drivermanager.java:243) at MySQL. Dbutil.connectaccess (dbutil.java:29) at MySQL. Dbutil.getconn (dbutil.java:15) at obj. Indexturnpic.getall (indexturnpic.java:49) at action. Getturnpic.doaction (getturnpic.java:17) at Maincontrol. Maincontrolservlet.service (maincontrolservlet.java:34) at Javax.servlet.http.HttpServlet.service ( httpservlet.java:728) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:305) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:210) at Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:222 ) at Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:123) at Org.apache.catalina.authenticator.AuthenticatorBase.invoke (authenticatorbase.java:472) at Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:171) at Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:99) at Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:118) at Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:408) at Org.apache.coyote.ajp.AjpProcessor.process (ajpprocessor.java:200) at org.apache.coyote.abstractprotocol$ Abstractconnectionhandler.process (abstractprotocol.java:589) at org.apache.tomcat.util.net.jioendpoint$ Socketprocessor.run (jioendpoint.java:310) at Java.util.concurrent.ThreadPoolExecutor.runWorker ( threadpoolexecutor.java:1145) at Java.util.concurrent.ThreadPoolExeCutor$worker.run (threadpoolexecutor.java:615) at Java.lang.Thread.run (thread.java:722) caused by: Com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link Failure

    Check it out. The application is connected to the MySQL database and the database is disconnected if the application does not access the database for more than 8 hours. An exception is thrown when the access is repeated.

    About MySQL automatic disconnection the results are as follows: In MySQL, there is a parameter setting, when the database connection is idle for a certain time, the server will disconnect the wait timeout.

    1. Relevant parameters, red part

    Mysql> Show variables like '%timeout% ';

    +--------------------------+-------+

    | variable_name | Value |

    +--------------------------+-------+

    | Connect_timeout | 5 |

    | Delayed_insert_timeout | 300 |

    | Innodb_lock_wait_timeout | 50 |

    | Interactive_timeout | 28800 |

    | Net_read_timeout | 30 |

    | Net_write_timeout | 60 |

    | Slave_net_timeout | 3600 |

    | Wait_timeout | 28800 |

    +--------------------------+-------+

    At the same time, only one of these two parameters works. Which parameter is in effect, related to the connection parameter specified when the user connects, by default, Wait_timeout is used. I recommend that you modify both parameters to avoid causing unnecessary trouble.

    2. Modify Parameters

    The default value for these two parameters is 8 hours (60*60*8=28800). I tested to change these two parameters to 0, the result is unexpectedly, the system automatically set this value to 1. In other words, the value cannot be set to permanent.

    Set these 2 parameters to one year (31536000).

    Set interactive_timeout= 31536000;

    Set wait_timeout=

    31536000

    ;

    You can also modify the MY.COF and re-start MySQL after modifying

    Open/etc/my.cnf, and add the following parameters under attribute group mysqld:

    [Mysqld]

    interactive_timeout= 31536000

    wait_timeout=

    31536000

    If there is no database access for a period of time, MySQL itself will sever the connection, and then access the Java Access connection pool when the data channel to the database is closed, because the DBCP connection pool cannot maintain the connection with the database at all times. MySQL5 even adding autoreconnect=true to the DBCP configuration will have no effect.

      • This article is from: Linux Tutorial Network

MySQL connection after a period of inactivity after an unexpected solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.