Internal Exception: java.sql.SQLException: Invalid state, the Connection object is closed

冷暖自知 提交于 2020-01-04 02:56:11

问题


I have a JSF application that uses Eclipse Persistence Services - 2.1.1.v20100817-r8050, and I sometimes get the following error:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.1.v20100817-r8050):    org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Invalid state, the Connection object is closed.
Error Code: 0
Call: SELECT XXXXX FROM XXXXX
bind => [/Home/Footer/]
Query: ReadAllQuery(name="WWW.find" referenceClass=WWW sql="SELECT XXXXX FROM XXXXX")

I have no idea what is causing it. The exception is thrown in the following function on the "return q.getResultList();" line.

public List<WWW> find(String url) {
    }
    EntityManager em = getEntityManager();
    try {
        Query q = em.createNamedQuery("WWW.find");
        q.setParameter("url", url);
        return q.getResultList();
    } finally {
        em.close();
    }
}

We have SQL Server 2005 and the database isn't down. This is more of a recent thing to consistently happen. Below is the properties that we set in the persistence.xml file. Any comments on what should change in this?

<properties>
  <property name="javax.persistence.jdbc.password" value="password"/>
  <property name="javax.persistence.jdbc.user" value="user"/>
  <property name="javax.persistence.jdbc.driver" value="net.sourceforge.jtds.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.url" value="jdbc:jtds:sqlserver://SERVER:1433/DB"/>
  <property name="eclipselink.ddl-generation" value="none"/>
  <property name="eclipselink.jdbc.timeout" value="20"/>
  <property name="eclipselink.jdbc.connections.wait-timeout" value="20"/>
  <property name="javax.persistence.query.timeout" value="20"/>
  <property name="eclipselink.allow-zero-id" value="true"/>
</properties>

Here is my edited class public class WWWJpaController {

public WWWWJpaController() {
    emf = Persistence.createEntityManagerFactory("properties");
}

public WWWWJpaController(String unitName) {
    emf = Persistence.createEntityManagerFactory(unitName);
}

private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {
    return emf.createEntityManager();
}

public List<WWW> find(String url) {
    EntityManager em = getEntityManager();
    try {
        Query q = em.createNamedQuery("WWW.find");
        q.setParameter("url", url);
        return q.getResultList();
    }finally{
        em.close();
    }
}

}

回答1:


My guess is your database connection has timed out, or your database is down. You may need to retry the query, or re-connect your session.

How have you configured your connection pooling and what database are you using?




回答2:


Maybe your aplication server is not able to reconnect to the database, as BalusC pointed out in his comment, this happens every time the backup's done.

You can make your server reconnect as pointed in this link for Jboss



来源:https://stackoverflow.com/questions/10845545/internal-exception-java-sql-sqlexception-invalid-state-the-connection-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!