java - 连接关闭后不允许进行任何操作

标签 java mysql hibernate jdbc

我从 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);
}

session 工厂:

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();
        }
    }
}

hibernate 和 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 block 中,并在出现错误时等待 10 秒并重新连接到数据库。

关于java - 连接关闭后不允许进行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27532036/

相关文章:

java - Hibernate session 无法打开事务

关于为多个枚举构建HashMap的Java问题

java - 为什么我无法使用 Graphics drawString 方法可视化字符串

java - 合并排序中的通用数组初始化

mysql - 在 mysql 中选择下一个/前 10 行

java - Hibernate 不一致地生成重复的主键

java - 服务器在一段时间后停止接受连接

PHP:使用字符串参数时 SQL 语句中的问题

php - 在简单表格中打印结果

java - 使用 IP 地址作为我的 Web 应用程序的 URL 时无法检索数据