Java 连接关闭后不允许进行任何操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27532036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No operations allowed after connection closed
提问by tuxx
I'm getting this error from a Java application:
我从 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!
The Java code:
Java代码:
try {
sessionFactory.beginTransaction();
//...
sessionFactory.commitTransaction();
} catch (Exception e) {
sessionFactory.rollbackTransaction();
throw new RuntimeException(e);
}
The session factory:
会话工厂:
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 and C3P0 settings:
休眠和 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>
Not sure how the connection can be still in use and this error thrown. What is causing this error?
不确定连接如何仍在使用中并抛出此错误。是什么导致了这个错误?
回答by Eric Leschinski
This Java method called executeQuery:
这个名为 executeQuery 的 Java 方法:
your_database_connection.createStatement().executeQuery(your_string_query);
will throw this Exception:
会抛出这个异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
No operations allowed after connection closed.
if you try to run it (executeQuery) after calling:
如果您在调用后尝试运行它 (executeQuery):
your_database_connection.close()
What is happening
怎么了
Either you are trying to get a connection to the database and are not getting one, and are trying to run a query against it which fails. Or you got a connection which suffered some problem and became closed, and you tried to run another query through it.
要么您正在尝试与数据库建立连接但没有获得连接,并且正在尝试对其运行失败的查询。或者您的连接遇到问题并关闭,您尝试通过它运行另一个查询。
The solution
解决方案
Assuming you lost the connection from network problems or something that isn't your fault, then put the code that attempts the query in a try/catch block, and on error wait 10 seconds and reconnect to the database.
假设您因网络问题或不是您的错而失去连接,然后将尝试查询的代码放在 try/catch 块中,并在出错时等待 10 秒并重新连接到数据库。
回答by jerry_li
your idleConnectionTestPeriod is 240,it should less then the db waiting timeout value ,see the db config ,check the value is bigger than 240.
您的 idleConnectionTestPeriod 是 240,它应该小于数据库等待超时值,请参阅数据库配置,检查该值是否大于 240。