在Apache Tomcat + MySQL上空闲一段时间后获取CommunicationsException

时间:2017-06-28 17:56:32

标签: mysql tomcat java-ee database-connection

我是J2EE的新手。

我在eclipse上编写了一个J2EE Web应用程序并将其移至我的Ubuntu 16.06,其中我安装了Apache Tomcat 8和MySQL 5.7(我使用的是mysql-connector-java-5.1.42-bin.jar)。

我在空闲时间(8)后反复出现以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
    at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1187)
    at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1182)
    at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4035)
    at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4004)
    .
    .
    .
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 55,475,050 milliseconds ago.  The last packet sent successfully to the server was 55,475,050 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:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3746)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2509)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2490)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
    at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
    at aut.ceit.isec.requestHandler.LoginServlet.doPost(LoginServlet.java:51)
    ... 32 more
Caused by: java.net.SocketException: Broken pipe (Write failed)
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3728)
    ... 40 more

我在SO上阅读了很多关于这个问题的类似帖子,但我无法解决我的问题。

我的context.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/db" type="javax.sql.DataSource"
    maxTotal="100" maxIdle="30" maxWaitMillis="10000"
    url="jdbc:mysql://localhost:3306/userdb?useEncoding=true&amp;characterEncoding=UTF-8"
    driverClassName="com.mysql.jdbc.Driver" username="root" password="xxxxxx"
    autoReconnect="true" defaultAutoCommit="true" validationQuery="select 1+1"
    testOnBorrow="true" />
</Context>

我的catalina.out文件中也有以下奇怪的信息(autoReconnect =&#34; true&#34; Tomcat在错误日志中推荐!!!):

Jun 28, 2017 2:20:52 PM org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory getObjectInstance
INFO: Name = db Ignoring unknown property: value of "true" for "autoReconnect" property

我应该配置像&#34; wait_timeout&#34;关于MySQL?

P.S。由于我的Tomcat和MySQL在同一台服务器上,我认为防火墙可能不是问题。

0 个答案:

没有答案