2012-07-26 49 views
0

我的代碼是用jruby編寫的,我使用warbler在tomcat中部署。我正在使用SEQUEL來查詢mysql數據庫。我正在使用兩層數據庫連接池。一個是本地SEQUEL池,另一個是在tomcat級別的JNDI池。連接字符串是:面對NativeException:java.sql.SQLException:連接[email protected]已關閉

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 [email protected] 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 

還有什麼可以創造差異?

回答

0

「通信鏈路故障」通常意味着您的程序無法連接到數據庫的所有。典型的問題是主機名和端口號不正確(這很容易檢查),或者在程序和數據庫之間存在一個或多個防火牆,包括軟件和硬件。

最好(如果可以的話)使用命令行mysql實用程序驗證是否可以從運行應用程序的計算機連接到數據庫服務器。如果有效,那麼你需要檢查你的<Resource>配置。如果它不起作用,那麼你需要研究一下防火牆的存在。

請注意,最新版本的Microsoft Windows(例如)默認啓用防火牆,可能不允許您的程序通過無法識別的端口(3306)建立外部連接。因此,請先檢查本地(即在運行webapp的計算機上)防火牆配置,然後與您的網絡管理員聯繫,以查看是否有其他防火牆妨礙了系統的正常工作。

相關問題