Home >Database >Mysql Tutorial >How to Fix \'MySQLNonTransientConnectionException: No operations allowed after connection closed\' in Remote Applications?

How to Fix \'MySQLNonTransientConnectionException: No operations allowed after connection closed\' in Remote Applications?

Linda Hamilton
Linda HamiltonOriginal
2024-10-30 13:11:02643browse

How to Fix

MySQL Non-Transient Connection Exception

Problem:

When deploying an application remotely, the exception "com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed" occurs after the application runs for over a day.

Cause:

The error is caused by a closed connection.

Potential Solutions:

  1. Connection Pooling:

    Hibernate's built-in connection pooling is limited. Consider using a third-party connection pooling solution such as C3P0.

    <code class="xml"><property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="c3p0.min_size">5</property>
    <property name="c3p0.max_size">20</property>
    <property name="c3p0.max_statements">50</property>
    <property name="c3p0.maxIdleTime">3600</property></code>
  2. Test Connection Before Use:

    Create a "c3p0.properties" file in the root of the classpath with the following property:

    <code class="properties">c3p0.testConnectionOnCheckout=true</code>

    This will test each connection before using it, which can help identify and close faulty connections.

Updated hibernate.cfg.xml Configuration:

<code class="xml"><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/xyz</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">50</property>

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.maxIdleTimeExcessConnections">3600</property>
<property name="c3p0.idleConnectionTestPeriod">3600</property>
<property name="c3p0.maxIdleTime">3600</property></code>

Note: Always test any configuration changes on a development or staging environment before deploying them to production.

The above is the detailed content of How to Fix \'MySQLNonTransientConnectionException: No operations allowed after connection closed\' in Remote Applications?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn