当前位置: 首页 > 知识库问答 >
问题:

Java语言sql。SQLRecoverableException:关闭的连接

时恩
2023-03-14

我正在使用Hibernate 3.3和Oracle 11g开发Struts 2 Framework。我的Web项目自5个月以来一直运行良好。但是最近我面临着java.sql.SQLRecoverableException:在一些空闲时间关闭连接。我将解释以下场景...我的hibernate.cfg.xml配置是

<property name="hibernate.current_session_context_class">
        thread
    </propert
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">user</property>
<property name="connection.password">user</property>
<property name="connection.driver_class">
        oracle.jdbc.OracleDriver
    </property>
<property name="myeclipse.connection.profile">Oracle</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="dialect"> org.hibernate.dialect.Oracle10gDialect </property>

我的HibernateSessionFactory配置是

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
    try {
        configuration.configure(configFile);
        sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
        System.err
                .println("%%%% Error Creating SessionFactory %%%%");
        //e.printStackTrace();
    }
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
    Session session = (Session) threadLocal.get();

    if (session == null || !session.isOpen()) {
        if (sessionFactory == null) {
            rebuildSessionFactory();
        }
        session = (sessionFactory != null) ? sessionFactory.openSession()
                : null;
        threadLocal.set(session);
    }

    return session;
}
public static void rebuildSessionFactory() {
    try {
        configuration.configure(configFile);
        sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
        System.err
                .println("%%%% Error Creating SessionFactory %%%%");
        //e.printStackTrace();
    }
}
public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);

    if (session != null) {
        session.close();
    }
}
public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
}
public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
}
public static Configuration getConfiguration() {
    return configuration;
}

然后在运行以下代码时出错

session = HibernateSessionFactory.getSession();
Query query = session.createQuery(SQL_QUERY);
try {
                // session.connection().close();
                System.out.println("CLOSED :"
                        + session.connection().isClosed());
                if (session.connection().isClosed()) {
                    System.out.println("RECONNECTING.......");
                    session.reconnect();
                }
                System.out.println("CLOSED :"
                        + session.connection().isClosed());
            } catch (Exception e1) {

                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
try {
for (Iterator it = query.iterate(); it.hasNext();) {
                    chk = true;
                    ur = (EmagEnterpriseLogin) it.next();
} catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

当上面的代码段运行时,即使已成功打开并创建了able to session查询,但在迭代查询时失败。第一次作为

Caused By:java.sql.SQLRecoverableException: Closed Connection

但是在第一次尝试中出现这个错误之前,我捕获了会话。Connection.isClosed()是错误的。但是在第一次错误之后,SQLState=null继续出现,从第二次开始,我可以得到会话。Connection.isClosed()是正确的。然后重新连接执行,但仍然重复相同的错误。我还尝试重建sesionFactory,但也失败了。请帮助我解决这个问题。

共有1个答案

有翰海
2023-03-14

最后,我发现我的问题的解决方案是使用第三方连接提供程序而不是使用Hibernate Native连接提供程序。我使用了c3p0连接提供程序,这也给我带来了一些问题,我也解决了。c3p0的参考链接在这里... Hibernate c3p0 Connection NewPooledConnection关闭异常

 类似资料:
  • 我编写了以下代码来连接到Oracle数据库。这用于REST API。我正在连接的数据库的IDLE_TIME参数设置为30分钟(这不能更改)。如果数据库连接空闲超过30分钟,则会抛出以下错误:“java.sql.SQLRecoverableException: ClosedConnection”。抛出此错误后,API将停止工作。我如何解决这个问题?

  • 我正在尝试使用Http将数据发布到REST服务,我已将我的客户端配置如下: 现在我有一个executor service负责调用实际发布,我将上述参数移交给executor service,如下所示: line是我试图发送的JSON负载。 现在,我在executor服务中的方法如下所示: 我在

  • 我已经将我的代码版本从http更改为https,我正在使用HttpClient=HttpClientFactory。getHttpsClient(),用于执行目的。当我第一次尝试运行代码时,它运行良好,下次抛出异常 Java语言lang.IllegalStateException:连接池关闭异常 我使用的是4.5HC。

  • 当从S3下载文件时,它失败了,给了我这个异常 Java语言lang.IllegalState异常:连接池关闭 com.amazonaws.http.conn.Aandler.invoke(Aactory.java:76) 在com.amazonaws.http.conn.AbstractConnProxy70.request(AbstractConnPorg.apache.http.impl.ex

  • 根据我所学到的,抽象是一个隐藏内部实现的概念。 在Java中,我们可以通过接口、抽象关键字(类/方法)以及方法来实现抽象。例如扫描器s=新扫描器(System.in);int x=s.nextint();这里我们不需要了解nextInt()方法的任何内容。所以我可以说这是抽象。 同样的事情也可以在C语言中用printf()实现 所以,如果即使在非OOP语言中也能实现抽象,那么为什么在基于OOP的语

  • 我最近转到了一个项目,在这个项目中我遇到了很多这种性质的代码--(这是使用jdbc postgres驱动程序) 显然,这段代码已经在生产中运行了一段时间,没有引起问题。 为了进一步澄清,如果我的理解是正确的(即,statement和resultset必须在连接关闭之前而不是之后关闭),我需要在catch和finally之间重复一些代码。修订后的代码如下所示。这可以简化吗? 只是为了透视,这段代码是