连接数据库超时设置autoReconnect=true

mysql经典的8小时问题

链自:

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

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

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

1,问题现象:

com.MySQL.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 seconds ago, which is longer than the server configured value of ‘wait_timeout‘. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property ‘autoReconnect=true‘ to avoid this problem.

解决办法: 
如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat. 
    不用hibernate的话, connection url加参数: autoReconnect=true 
    用hibernate的话, 加如下属性: 
        <property name="connection.autoReconnect">true</property> 
        <property name="connection.autoReconnectForPools">true</property> 
        <property name="connection.is-connection-validation-required">true</property> 
    要是还用c3p0连接池: 
        <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>

以上转载链接地址:http://blog.aqsc.cn/article.php?type=blog&itemid=1016

2,另外:关于Mysql链接参数的说明如下:

mysql JDBC Driver

常用的有两个,一个是gjt(Giant JavaTree)组织提供的mysql驱动,其JDBC Driver名称(Java类名)为:org.gjt.mm.mysql.Driver

详情请参见网站:http://www.gjt.org/

或在本网站下载mysql JDBC Driver(mm.jar)

另一个是mysql官方提供的JDBC Driver,其JAVA类名为:com.mysql.jdbc.Driver

驱动下载网址:http://dev.mysql.com/downloads/,进入其中的MySQL Connector/J区域下载。

mysql JDBC URL格式如下:

jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值2]...

现只列举几个重要的参数,如下表所示:

参数名称 参数说明 缺省值 最低版本要求
user 数据库用户名(用于连接数据库)   所有版本
password 用户密码(用于连接数据库)   所有版本
useUnicode 是否使用Unicode字符集,如果参数characterEncoding设置为gb2312或gbk,本参数值必须设置为true false 1.1g
characterEncoding 当useUnicode设置为true时,指定字符编码。比如可设置为gb2312或gbk false 1.1g
autoReconnect 当数据库连接异常中断时,是否自动重新连接? false 1.1
autoReconnectForPools 是否使用针对数据库连接池的重连策略 false 3.1.3
failOverReadOnly 自动重连成功后,连接是否设置为只读? true 3.0.12
maxReconnects autoReconnect设置为true时,重试连接的次数 3 1.1
initialTimeout autoReconnect设置为true时,两次重连之间的时间间隔,单位:秒 2 1.1
connectTimeout 和数据库服务器建立socket连接时的超时,单位:毫秒。 0表示永不超时,适用于JDK 1.4及更高版本 0 3.0.1
socketTimeout socket操作(读写)超时,单位:毫秒。 0表示永不超时 0 3.0.1

对应中文环境,通常mysql连接URL可以设置为:

jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false

在使用数据库连接池的情况下,最好设置如下两个参数:

autoReconnect=true&failOverReadOnly=false

需要注意的是,在xml配置文件中,url中的&符号需要转义成&。比如在tomcat的server.xml中配置数据库连接池时,mysql jdbc url样例如下:

jdbc:mysql://localhost:3306/test?user=root&amp;password=&amp;useUnicode=true&amp;characterEncoding=gbk

&amp;autoReconnect=true&amp;failOverReadOnly=false

其他参数请参见mysql jdbc官方文档:MySQL Connector/J Documentation

时间: 2024-08-29 20:39:07

连接数据库超时设置autoReconnect=true的相关文章

jdbc autoReconnect=true 参数设置导致 slow log 爆表。

1.过程 同事按照文档上配置了下面的jdbc url: jdbc:mysql://ip:port/db?autoReconnect=true&useUnicode=true&characterEncoding=utf-8 结果导致了 mysql slow log 出现了满屏的 ping 命令, # [email protected]: db[db] @ [ip] # Query_time: 0.000017 Lock_time: 0.000000 Rows_sent: 0 Rows_exa

日志:using the Connector/J connection property &#39;autoReconnect=true&#39; to avoid this problem

com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 seconds ago, which is longer than the server configured value of 'wait_timeou

Outlook Web App 客户端超时设置

这篇文章我们讨论一下,OWA 2013在公共和私人的电脑是如何启用和配置. Exchange 2013 Outlook Web App (OWA) 登录页不再允许用户选择无论他们正在使用公共的或私人的计算机.默认情况下,OWA 2013 是假定用户使用的是私人计算机,如使用超时的 8 小时.出于安全考虑,用户处于非活动状态之前要求用户重新登录. 在 Set-OWAVirtualDirectory cmdlet 中 LogonPagePublicPrivateSelectionEnabled 参数

你应该知道的那些超时设置或默认参数 good

defaultStatementTimeout Sets the number of seconds the driver will wait for a response from the database. Any positive integer Not Set (null) http://www.mybatis.org/mybatis-3/configuration.html 存储介质 Redis Java-Driver Jedis 连接池:Jedis的连接池设计基于 Apache Co

基础入门_Python-模块和包.Gevent异步/状态获取/超时设置/猴子补丁?

简单介绍: 说明: Gevent是一个基于libev的并发库,为各种并发和网络相关的任务提供了整洁的API 快速安装: pip install --upgrade gevent 主要模式: 说明: Greenlet以C扩展模块形式接入PY轻量级协程,它们运行于主进程内部,被协作式的调度,且不同于multiprocessing和threading等真正的并行执行,它在同一时刻只能有一个协程在运行 公有方法 gevent.spawn(cls, *args, **kwargs) 创建一个Greenle

Socket编程实践(9) --套接字IO超时设置方法

引:超时设置3种方案 1. alarm超时设置方法 //代码实现: 这种方式较少用 void sigHandlerForSigAlrm(int signo) { return ; } signal(SIGALRM, sigHandlerForSigAlrm); alarm(5); int ret = read(sockfd, buf, sizeof(buf)); if (ret == -1 && errno == EINTR) { // 超时被时钟打断 errno = ETIMEDOUT;

commons-httpclient中的超时设置

connectionTimeout与soTimeout的差异,前者指创建一个有效的客户端到服务端链接的最大允许时间,后者指socket接收data的时间. connectionManager.getParams().setConnectionTimeout(50); connectionManager.getParams().setSoTimeout(100); 调用connectionTimeout属性的代码: //org.apache.commons.httpclient.protocol.

http超时设置

http协议中可以设置超时(连接超时.响应超时),具体设置情景如下: 1.java api的方式: JDK 1.5以前的版本,只能通过设置两个系统属性来控制网络超时: String szUrl = "http://www.ee2ee.com/"; URL url = new URL(szUrl); HttpURLConnection urlCon = (HttpURLConnection)url.openConnection(); //HttpURLConnection是基于HTTP协

Confluence 6 配置 HTTP 超时设置

当宏,例如 RSS Macro 进行 HTTP 请求的时候,有可能因为请求的时间比较长,而导致超时.你可以通过设置系统参数来避免这个问题. 配置 HTTP 超时设置: 在屏幕的右上角单击 控制台按钮 ,然后选择 General Configuration 链接. 在左侧面板的顶部选择 基本配置(General Configuratio). 在屏幕的下方找到 链接超时(Connection Timeouts)选项.Find the '' section in the lower portion o