gpt4 book ai didi

jdbc - 由于 : java. io.EOFException 导致通信链路失败

转载 作者:行者123 更新时间:2023-12-01 06:36:29 26 4
gpt4 key购买 nike

我的 webapp 在 Tomcat 5.5 上运行,我在 web.xml 中声明了数据源:

<resource-ref>
<res-ref-name>jdbc/OrdiniWebDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

在 context.xml (tomcat conf) 中:

<Resource 
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/OrdiniWebDS"
password="[mypassword]"
type="javax.sql.DataSource"
url="jdbc:mysql://[myHost:port]/ordiniweb"
username="[myusername]"
/>

数据库是MySql 5.0。一切正常,除了有时,在“未使用”几个小时后,第一次访问时我得到了这个异常:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION **

java.io.EOFException

STACKTRACE:

java.io.EOFException
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2368)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
at com.blasetti.ordiniweb.dao.OrdiniDAO.caricaOrdine(OrdiniDAO.java:263)
...
** END NESTED EXCEPTION **



Last packet sent to the server was 0 ms ago.
com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2579)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708)
com.mysql.jdbc.Connection.execSQL(Connection.java:3255)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293)
com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428)
org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
com.blasetti.ordiniweb.dao.OrdiniDAO.caricaOrdine(OrdiniDAO.java:263)
...

我需要刷新,它再次运行良好。任何想法?谢谢。

最佳答案

MySQL 会在一段时间后丢弃未使用的连接,因为它假定对方忘记关闭它。

您需要做的是配置一个 Tomcat 在重用池连接之前应该应用的检查。为此,请将 ?autoReconnect=true&useUnicode=true&characterEncoding=utf8 添加到 URL 的末尾,并将 validationQuery="Select 1" 添加到 Resource 元素:

<Resource 
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/OrdiniWebDS"
password="[mypassword]"
type="javax.sql.DataSource"
url="jdbc:mysql://[myHost:port]/ordiniweb?autoReconnect=true&useUnicode=true&characterEncoding=utf8"
username="[myusername]"
validationQuery="Select 1"
/>

[编辑] 此页面提供更多详细信息:Configuring a MySQL Datasource in Apache Tomcat

关于jdbc - 由于 : java. io.EOFException 导致通信链路失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4436997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com