gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:33:44 24 4
gpt4 key购买 nike

我从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com