面对NativeException:java.sql.SQLException:连接com.mysql.jdbc.JDBC4Connection@38054ba0已关闭

时间:2012-07-26 09:15:11

标签: mysql tomcat jruby jndi sequel

我的代码是用jruby编写的,我使用warbler在tomcat中部署。我正在使用SEQUEL来查询mysql数据库。我正在使用两层数据库连接池。一个是本地SEQUEL池,另一个是在tomcat级别的JNDI池。连接字符串是:

DB = Sequel.connect("jdbc:jndi:java:comp/env/test", :logger => $db_log, :max_connections => 10)

此连接字符串在app.rb中定义,只有在重新启动部署或重新启动tomcat时才会加载该连接字符串。这将创建续集连接池,并且所有线程共享此池。 我的$ CATALINA_OME / conf / context.xml中的JNDI配置如下:

<Resource
name="test"
auth="Container"
type="javax.sql.DataSource"
maxActive="10"
maxIdle="5"
maxWait="9000"
username="test_db"
password="test_db"
driverClassName="com.mysql.jdbc.Driver"
testOnBorrow="true"
testWhileIdle="true"
validationQuery="SELECT 1" testOnReturn="true"
timeBetweenEvictionRunsMillis="300000" removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
url="jdbc:mysql://IP:3306/test"
/>

我正在使用DB.disconnect返回与JNDI池的连接,以确保没有线程使用先前使用过的连接。我这样做是为了确保wait_timeout错误。 “auto_reconnect = true”似乎无法正确解决wait_timeout问题。 一切都工作正常,直到几天我突然开始出现错误,如:

W, [2012-07-26T05:10:30.999000 #29456]  WARN -- : 134325951259087 == NativeException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
D, [2012-07-26T05:10:31.001000 #29456] DEBUG -- : 134325951259087 == ["sun.reflect.GeneratedConstructorAccessor23:-1:in `newInstance'", "sun/reflect/DelegatingConstructorAccessorImpl.java:27:in `newInstance'", "java/lang/reflect/Constructor.java:513:in `newInstance'", "com/mysql/jdbc/Util.java:409:in `handleNewInstance'", "com/mysql/jdbc/SQLError.java:1118:in `createCommunicationsException'", "com/mysql/jdbc/MysqlIO.java:343:in `<init>'", "com/mysql/jdbc/ConnectionImpl.java:2308:in `connectOneTryOnly'", "com/mysql/jdbc/ConnectionImpl.java:2122:in `createNewIO'", "com/mysql/jdbc/ConnectionImpl.java:774:in `<init>'", "com/mysql/jdbc/JDBC4Connection.java:49:in `<init>'", "sun.reflect.GeneratedConstructorAccessor25:-1:in `newInstance'", "sun/reflect/DelegatingConstructorAccessorImpl.java:27:in `newInstance'", "java/lang/reflect/Constructor.java:513:in `newInstance'", "com/mysql/jdbc/Util.java:409:in `handleNewInstance'"

&安培;

W, [2012-07-26T10:19:14.029000 #1572]  WARN -- : 134327815053548 == NativeException: java.sql.SQLException: Connection com.mysql.jdbc.JDBC4Connection@5a0655d is closed.
D, [2012-07-26T10:19:14.030000 #1572] DEBUG -- : 134327815053548 == ["org/apache/tomcat/dbcp/dbcp/DelegatingConnection.java:398:in `checkOpen'", "org/apache/tomcat/dbcp/dbcp/DelegatingConnection.java:255:in `createStatement'", "file:/usr/local/tomcat-instance/test/webapps/service/WEB-INF/lib/santa-gems.jar!/gems/sequel-3.34.0/lib/sequel/adapters/jdbc.rb:523:in `statement'", "file:/usr/local/tomcat-instance/test/webapps/service/WEB-INF/lib/santa-gems.jar!/gems/sequel-3.34.0/lib/sequel/adapters/jdbc.rb:233:in `execute'", "file:/usr/local/tomcat-instance/test/webapps/service/WEB-INF/lib/santa-gems.jar!/gems/sequel-3.34.0/lib/sequel/connection_pool/threaded.rb:88:in `hold'", "file:/usr/local/tomcat-instance/test/webapps/service/WEB-INF/lib/santa-gems.jar!/gems/sequel-3.34.0/lib/sequel/database/connecting.rb:234:in `synchronize'", "file:/usr/local/tomcat-instance/testv/webapps/service/WEB-INF/lib/santa-gems.jar!/gems/sequel-3.34.0/lib/sequel/adapters/jdbc.rb:232:in `execute'", "file:/usr/local/tomcat-instance/test/webapps/service/WEB-INF/lib/santa-gems.jar!/gems/sequel-3.34.0/lib/sequel/dataset/actions.rb:744:in `execute'", "file:/usr/local/tomcat-instance/test/webapps/service/WEB-INF/l

并且,这些错误发生得太频繁而无法忽略。 在没有任何错误的时间和现在是数据库主机的更改之间发生的唯一更改。但我确保超时变量保持不变。值为:

interactive_timeout=300
connect_timeout=300
wait_timeout=10

还有什么可以产生差异吗?

1 个答案:

答案 0 :(得分:0)

“通信链接失败”通常意味着您的程序无法连接到数据库 。典型的问题是不正确的主机名和端口号(很容易仔细检查),或者在程序和数据库之间存在一个或多个防火墙,包括软件和硬件。

最好(如果可以)使用命令行mysql实用程序来验证您是否可以从运行应用程序的计算机连接到数据库服务器。如果可以,那么您需要检查<Resource>配置。如果它不起作用,那么你需要研究是否存在防火墙。

请注意,最新版本的Microsoft Windows(例如)默认启用防火墙,这可能不允许程序通过无法识别的端口进行外部连接(3306)。因此,首先检查您的本地(即在运行webapp的计算机上)防火墙配置,然后与您的网络管理员联系,看看是否有其他防火墙,如果不能正常工作。