我从Java连接MySQL服务器时遇到问题。它是一个远程MySQL服务器。我正在使用Glassfish 4.我使用连接池和jdbc连接到数据库。
闲置几分钟后,连接链路断开。服务器尝试重新连接几次,但失败,然后抛出Communications link failure
异常。这是完整的堆栈跟踪:
Info: Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 580,803 milliseconds ago. The last packet sent successfully to the server was 21,302 milliseconds ago.
Error Code: 0
Call: SELECT payload FROM session WHERE (id = ?)
bind => [1 parameter bound]
Query: ReportQuery(name="Session.findAuthorBySession" referenceClass=Session sql="SELECT payload FROM session WHERE (id = ?)").
Info: Communication failure detected when attempting to perform read query outside of a transaction. Attempting to retry query. Error was: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
Error Code: 0
这里尝试重新连接几次(10-15次),然后抛出:
Query: ReportQuery(name="Session.findAuthorBySession" referenceClass=Session sql="SELECT payload FROM session WHERE (id = ?)").
Warning: Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
Error Code: 0
Call: SELECT payload FROM session WHERE (id = ?)
bind => [1 parameter bound]
Query: ReportQuery(name="Session.findAuthorBySession" referenceClass=Session sql="SELECT payload FROM session WHERE (id = ?)")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processExceptionForCommError(DatabaseAccessor.java:1611)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:674)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002)
at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2738)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllReportQueryRows(ExpressionQueryMechanism.java:2675)
at org.eclipse.persistence.queries.ReportQuery.executeDatabaseQuery(ReportQuery.java:848)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1127)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1215)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1874)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:687)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5538)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1840)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1874)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:687)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5538)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1840)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1874)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:687)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5538)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1840)
at org.eclipse.persistence.internal.sessions.AbstractSession.retryQuery(AbstractSession.java:1874)
at org.eclipse.persistence.sessions.server.ClientSession.retryQuery(ClientSession.java:687)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.retryQuery(UnitOfWorkImpl.java:5538)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1840)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1751)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
at org.eclipse.persistence.internal.jpa.QueryImpl.getSingleResult(QueryImpl.java:517)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:400)
注意: 奇怪的是:我使用localhost测试一些新功能,然后部署到Jelastic云。有这个问题没有发生(但这可能发生,因为一些差异,如IP被更改为机器的本地主机等)。