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:
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>
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!