Connection Database Timeout setting autoreconnect=true

Source: Internet
Author: User
Tags connection pooling documentation

MySQL Classic 8-hour problem

Chain from:

http://blog.csdn.net/bluesnail216/article/details/15810119

Reference http://www.wang1314.com/doc/topic-1520183-1.html

Http://www.jb51.net/article/32284.htm

1, problem phenomenon:

Com.MySQL.jdbc.CommunicationsException:The last packet successfully received from the server was58129 seconds ago. The last packet sent successfully to the server were 58129 seconds ago, which is longer than the server configured value of ' Wait_timeout '. Should consider either expiring and/or testing connection validity before use in your application, increasing the Serv Er configured values for client timeouts, or using the Connector/j Connection property ' Autoreconnect=true ' to avoid this Problem.

Workaround:
If the connection is idle for 8 hours (no database operation within 8 hours), MySQL will automatically disconnect and restart Tomcat.
Without hibernate, connection URL plus parameter: autoreconnect=true
With Hibernate, add the following attributes:
<property name= "Connection.autoreconnect" >true</property>
<property name= "Connection.autoreconnectforpools" >true</property>
<property name= "connection.is-connection-validation-required" >true</property>
If you also use the C3P0 connection pool:
<property name= "Hibernate.c3p0.acquire_increment" >1</property>
<property name= "Hibernate.c3p0.idle_test_period" >0</property>
<property name= "Hibernate.c3p0.timeout" >0</property>
<property name= "Hibernate.c3p0.validate" >true</property>

The above reprint link address: http://blog.aqsc.cn/article.php?type=blog&itemid=1016

2, in addition: a description of MySQL link parameters is as follows:

MySQL JDBC Driver

There are two commonly used, one is the MySQL driver provided by GJT (Giant Java Tree) organization, its JDBC Driver name (Java class name) is: Org.gjt.mm.mysql.Driver

For details, see website: http://www.gjt.org/

or download MySQL JDBC Driver (mm.jar) on this website

The other is the official MySQL-provided JDBC Driver, whose Java class name is: Com.mysql.jdbc.Driver

Driver Download URL: http://dev.mysql.com/downloads/, enter the MySQL connector/j area to download.

The MySQL JDBC URL format is as follows:

jdbc:mysql://[host:port],[host:port].../[database][. Parameter name 1][= argument value 1][& parameter name 2][= argument value 2] ...

Only a few important parameters are listed, as shown in the following table:

Parameter name Parameter description Default value Minimum version requirements
User Database user name (used to connect to database) All versions
Password User password (used to connect to database) All versions
Useunicode If the Unicode character set is used, the value of this parameter must be set to True if the parameter characterencoding is set to gb2312 or GBK False 1.1g
Characterencoding When Useunicode is set to True, the character encoding is specified. For example, can be set to gb2312 or GBK False 1.1g
AutoReConnect Is the connection automatically reconnected when the database connection is interrupted abnormally? False 1.1
Autoreconnectforpools Whether to use a reconnection policy for database connection pooling False 3.1.3
Failoverreadonly Is the connection set to read-only after the automatic reconnection is successful? True 3.0.12
Maxreconnects When AutoReConnect is set to true, the number of times to retry the connection 3 1.1
Initialtimeout When AutoReConnect is set to true, the time interval between two re-interconnects, in seconds 2 1.1
ConnectTimeout Time-out, in milliseconds, when establishing a socket connection with the database server. 0 means never timeout, for JDK 1.4 and later 0 3.0.1
Sockettimeout Socket operation (Read-write) timeout, in milliseconds. 0 means never time out 0 3.0.1

For the Chinese environment, the MySQL connection URL can usually be set to:

jdbc:mysql://localhost:3306/test?user=root&password=&useunicode=true&characterencoding=gbk& Autoreconnect=true&failoverreadonly=false

In the case of using a database connection pool, it is best to set the following two parameters:

Autoreconnect=true&failoverreadonly=false

Note that in the XML configuration file, the & symbol in the URL needs to be escaped to &. For example, when configuring a database connection pool in Tomcat's server.xml, the MySQL JDBC URL sample is as follows:

Jdbc:mysql://localhost:3306/test?user=root&amp;password=&amp;useunicode=true&amp;characterencoding =gbk

&amp;autoreconnect=true&amp;failoverreadonly=false

For additional parameters, see MySQL JDBC official documentation: MySQL connector/j documentation

Connection Database Timeout setting autoreconnect=true

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.