关闭连接后不允许任何操作

我从Java应用程序中收到这个错误:

com.mchange.v2.c3p0.impl.NewPooledConnection - [c3p0] Another 
error has occurred [     
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: 
No operations allowed after connection closed. ] which will not be 
reported to listeners!

Java代码:

try {
    sessionFactory.beginTransaction();
    //...
    sessionFactory.commitTransaction();
} catch (Exception e) {
    sessionFactory.rollbackTransaction();
    throw new RuntimeException(e);
}

会话工厂:

try {
    sessionFactory.beginTransaction();
    ...
    sessionFactory.commitTransaction();
} catch (Exception e) {
    sessionFactory.rollbackTransaction();
    throw new RuntimeException(e);
}

public Transaction beginTransaction() throws HibernateException {
    try {
        return sessionFactory.getCurrentSession().beginTransaction();
    } catch (HibernateException hex) {
        LOG.error(String.format("Unable to start database transaction due to exception: %s.", ExceptionUtils.getRootCauseMessage(hex)));
        throw hex;
    }
}

public void commitTransaction() throws HibernateException {
    LOG.debug("Committing database transaction.");
    try {
        if (sessionFactory.getCurrentSession().getTransaction().isActive()) {
            sessionFactory.getCurrentSession().getTransaction().commit();
        } else {
            throw new HibernateException("Transaction is no longer Active.");
        }
    } catch (HibernateException hex) {
        LOG.error(String.format("Unable to commit due to exception: %s.", ExceptionUtils.getRootCauseMessage(hex)));
        throw hex;
    }
}

public void rollbackTransaction() throws HibernateException {
    LOG.debug("Trying to rollback database transaction after exception.");
    Session session = sessionFactory.getCurrentSession();
    try {
        if (session.getTransaction().isActive()) {
            session.getTransaction().rollback();
        } else {
            throw new HibernateException("Transaction is no longer Active.");
        }
    } catch (HibernateException hex) {
        LOG.error(String.format("Unable to rollback due to exception: %s.", ExceptionUtils.getRootCauseMessage(hex)));
        throw hex;
    } finally {
        if (session.isOpen()) {
            session.close();
        }
    }
}

休眠和C3P0设置:

<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.c3p0.timeout">1800 <!-- seconds --></prop>
<prop key="hibernate.c3p0.min_size">4</prop>
<prop key="hibernate.c3p0.max_size">35</prop>
<prop key="hibernate.c3p0.idle_test_period">240 <!-- seconds --></prop>
<prop key="hibernate.c3p0.acquire_increment">4</prop>
<prop key="hibernate.c3p0.max_statements">0</prop>
<prop key="hibernate.c3p0.preferredTestQuery">SELECT 1</prop>
<prop key="c3p0.testConnectionOnCheckout">true</prop>
<prop key="hibernate.connection.oracle.jdbc.ReadTimeout">60000 <!-- milliseconds --></prop>

不确定连接如何继续使用,并抛出此错误。 什么导致了这个错误?


这个名为executeQuery的Java方法:

your_database_connection.createStatement().executeQuery(your_string_query);

会抛出这个异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: 
No operations allowed after connection closed.

如果您尝试在调用后运行它(executeQuery):

your_database_connection.close()

发生什么事

要么你正在试图获得与数据库的连接,而是没有得到一个连接,并试图对其执行失败的查询。 或者你有一个连接遇到了一些问题并且关闭了,并且你试图通过它运行另一个查询。

解决方案

假设您从网络问题或某些非您的错误中失去了连接,则将尝试查询的代码放入try / catch块中,并等待10秒钟,然后重新连接到数据库。


你的idleConnectionTestPeriod是240,它应该少于那个db等待超时值,看到db config,检查这个值是否大于240。

链接地址: http://www.djcxy.com/p/23915.html

上一篇: No operations allowed after connection closed

下一篇: Appended div couldn't scroll