I have a problem with my connection with MySQL server from Java. It is a remote MySQL server. I'm using Glassfish 4. I'm using connection pooling and jdbc to connect to the database.
After a few minutes of idle, the connection link is broken. The server tries to reconnect a couple of times, but fails, then throws Communications link failure
exception. Here is the full stack trace:
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
Here it tries to reconnect a couple more times (10-15 more times), then it throws this:
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)
Note: The weird thing is: I'm using localhost to test some new features then deploy to Jelastic cloud. There this problem does not occur (but this can happen because of some differences like the ip being changed to the localhost of the machine etc).