在几秒钟之后,我们收到一个CommunicationsException(来自DBCP).错误消息(在异常中)是在此问题的结尾 – 但我没有看到任何配置文件中定义的wait_timeout. (我们应该在哪里看?tomcat / conf目录中的某个地方?).
其次,正如Exception所建议的那样,将“Connector / J connection属性”autoReconnect = true’“放在哪里?这是Tomcat中文件conf / context.xml中的资源定义设置:
<Resource name="jdbc/TomcatResourceName" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true" username="xxxx" password="yyyy" driverClassName="com.MysqL.jdbc.Driver" url="jdbc:MysqL://127.0.0.1:3306/dbname?autoReconnect=true"/>
第三,为什么JVM等待调用executeQuery()来抛出异常?如果连接超时,getConnection方法应该抛出异常,不应该吗?这是我在谈论的源代码部分:
try { conn = getConnection (true); stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rset = stmt.executeQuery (bQuery); while (rset.next()) { ....
最后,这里是堆栈跟踪的第一行
com.MysqL.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 84,160,724 milliseconds ago. The last packet sent successfully to the server was 84,848 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 this problem. 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:532) at com.MysqL.jdbc.Util.handleNewInstance(Util.java:406) at com.MysqL.jdbc.sqlError.createCommunicationsException(sqlError.java:1074) at com.MysqL.jdbc.MysqLIO.send(MysqLIO.java:3291) at com.MysqL.jdbc.MysqLIO.sendCommand(MysqLIO.java:1938) at com.MysqL.jdbc.MysqLIO.sqlQueryDirect(MysqLIO.java:2107) at com.MysqL.jdbc.ConnectionImpl.execsql(ConnectionImpl.java:2642) at com.MysqL.jdbc.ConnectionImpl.execsql(ConnectionImpl.java:2571) at com.MysqL.jdbc.StatementImpl.executeQuery(StatementImpl.java:1451) at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
这些是我们一些人认为“忘记dbcp”的原因,它可能依赖于IDE配置和引擎下的魔术,DriverManager.getConnection(…)可能更可靠“.有什么意见吗?谢谢你的见解 – MS
解决方法
由于DBCP保持返回的MysqL连接打开即将到来的连接请求,它们成为
MySQL Server timeout的受害者.
DBCP有许多可以帮助的功能(可以从Tomcat 5.5 IIRC开始使用).
validationQuery="SELECT 1" testOnBorrow="true"
验证确保连接在返回到执行“借用”方法的webapp之前是有效的.标志当然可以启用此功能.
如果超时(8小时我相信)已经过去,并且连接已经死了,那么会测试一个新的连接(如果没有了,它被创建)并提供给webapp.
其他可能的方法:
>在资源设置中使用testWhileIdle =“true”DBCP,在检测到有效请求之前也检查空闲连接.>使用’connectionProperties’硬化你的MysqL连接(例如autoReconnect / autoReconnectForPools = true)