使用DBCP进行Tomcat配置

我们在嘲笑了一段时间(几个小时)后得到了一个CommunicationsException(来自DBCP)。 错误消息(在Exception中)是在这个问题的结尾 – 但我没有看到在任何配置文件中定义wait_timeout。 (我们应该在哪里看?在tomcat / conf目录之外的某个地方?)。

其次,正如Exception所建议的那样,在哪里放置“Connector / J连接属性’autoReconnect = true’”? 这是tomcat设置文件conf / context.xml中的资源定义:

 

第三,为什么JVM会等到executeQuery()调用抛出exception? 如果连接超时,getConnection方法应抛出exception,不应该吗? 这是我正在讨论的源代码部分:

  try { conn = getConnection (true); stmt = conn.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rset = stmt.executeQuery (bQuery); while (rset.next()) { .... 

最后,这里是Stack跟踪的前几行……

 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,160,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服务器超时的受害者。

DBCP具有许多可以提供帮助的function(可以从Tomcat 5.5 IIRC开始使用)。

 validationQuery="SELECT 1" testOnBorrow="true" 

validation确保连接有效,然后将其返回到执行’borrow’方法的webapp。 当然,标志启用此function。

如果超时(我相信8小时)已经过去并且连接已经死亡,那么将测试新连接(如果已经没有,则创建它)并提供给webapp。

其他可能的方法:

  1. 在资源设置中使用testWhileIdle="true" DBCP,还可以在检测到有效请求之前检查空闲连接。

  2. 使用’connectionProperties’来强化你的MySQL连接(例如autoReconnect/autoReconnectForPools=true

即使作者说,DBCP也不适合用于制作(参见此演示文稿: http : //www.infoq.com/presentations/Tuning-Tomcat-Mark-Thomas )。

我建议看看C3P0: http : //www.mchange.com/projects/c3p0/index.html