Postgresql + Hibernate3 + C3p0 = not closing connections
This only happens until peak load of the system.
Hibernate exception:
org.hibernate.exception.JDBCConnectionException: could not execute query
org.postgresql.util.PSQLException: An I/O error occured while sending to the backend. java.io.IOException: Stream closed
postgresql log entry: 2011-08-10 05:27:35 UTC LOG: unexpected EOF on clien
t connection
Here is my hibernate xml file
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://[url]/sa_serve开发者_StackOverflowr</property>
<property name="connection.username">[user]</property>
<property name="connection.password">[pw]</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">5</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernatespatial.postgis.PostgisDialect</property>
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
EDIT----
Here is the code I am calling to get and close the session.
// perform the operation itself
try {
session = AppSessionFactory.openSession();
session.beginTransaction();
session.setFlushMode( FlushMode.COMMIT );
if ( pre() ) {
if ( doWork() ) {
if ( post() ) {
session.getTransaction().commit();
}
}
}
if ( !response.success ) {
session.getTransaction().rollback();
}
} catch ( Exception e ) {
if ( session != null ) {
try { session.getTransaction().rollback(); } catch ( Exception e2 ) {}
}
// attempt to retrieve a useful error for the invoker
outerException = e;
Throwable cause = outerException.getCause();
if ( cause != null && cause instanceof SQLException ) {
Exception rootCause = ((SQLException)cause).getNextException();
if ( rootCause != null ) {
innerException = rootCause;
}
}
} finally {
if ( session != null ) { session.close(); }
}
EDIT--
public class AppSessionFactory {
private static AppSessionFactory instance = null;
private final SessionFactory sessionFactory =
new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
private AppSessionFactory() {}
@Override
protected void finalize() {
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
private static AppSessionFactory instance() {
if ( instance == null ) {
instance = new AppSessionFactory();
}
return instance;
}
public static Session openSession() {
return instance().sessionFactory.openSession();
}
}
We had the same problem and switched to BoneCP. Everything is running fine now. We have a website with high load and peaks of 3000 pageviews/second. The solution by kgibbon was tried to but didn't worked for us.
solution: added testConnectionOnCheckout=true and testConnectionOnCheckin=true to c3p0 config.
精彩评论