mysql连接ODBC数据源超时_mysql连接超时与jndi数据源配置

昨天有运营说添加活动不能用了,我就看了一下后台日志,发现访问数据库是报错:

at java.lang.Thread.run(Thread.java:722)

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was59,085,803 milliseconds ago.

The last packet sent successfully to the server was 59,085,806 milliseconds ago.

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 thisproblem.

at sun.reflect.GeneratedConstructorAccessor377.newInstance(Unknown Source)

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:407)

at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)

at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3358)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1970)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)

意思就是数据库有长连接,长时间没有使用数据库,并且空闲时间超过了数据库配置的wait_timeout设置的时间。到了wait_timeout的时间,数据库就会释放连接。一般的思路是,改数据库的wait_timeout时间,这个方法可行,但是并不是最好的

因为,mysql的最大超时时间是2147483。所以我们的逻辑就要转到mysql链接来

(1) JNDI数据源:

我们使用的是jndi数据源配置如图:

/>

需要添加如下的连接测试

validationQuery = "SELECT 1"testWhileIdle= "true"timeBetweenEvictionRunsMillis= "3600000"minEvictableIdleTimeMillis= "18000000"testOnBorrow= "true"

(2)jdbc数据源配置

jdbc:mysql://10.10.10.10:3306/mydb?autoReconnect=true

(3)Spring中使用DBCP连接池,在定义datasource增加属性validationQuery和testOnBorrow

(4)

如果是在Spring中使用c3p0连接池,则在定义datasource的时候,添加属性testConnectionOnCheckin和testConnectionOnCheckout,如:

2.jndi数据源配置

(1) 什么是jndi

http://blog.csdn.net/liujiahan629629/article/details/20568475

http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html

http://blog.csdn.net/jiangguilong2000/article/details/12523771

http://blog.csdn.net/beijixingtianyake/article/details/49927761

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值