昨天有運營說添加活動不能用了,我就看了一下后台日志,發現訪問數據庫是報錯:
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