Postgresql + Hibernate3 + C3p0 =没有关闭连接

时间:2011-08-10 05:51:34

标签: hibernate postgresql c3p0

这只发生在系统的峰值负载之前。

Hibernate异常:

org.hibernate.exception.JDBCConnectionException: could not execute query

org.postgresql.util.PSQLException:发送到后端时发生I / O错误。 java.io.IOException:Stream closed

postgresql log entry: 2011-08-10 05:27:35 UTC LOG:  unexpected EOF on clien

t连接

这是我的hibernate xml文件

<!-- Database connection settings -->
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://[url]/sa_server</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 ----

这是我打电话来获取和关闭会话的代码。

// 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(); }
    }

编辑 -

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();
}

}

2 个答案:

答案 0 :(得分:1)

我们遇到了同样的问题,并转而使用BoneCP。现在一切都很顺利。我们有一个网站负载很高,峰值为3000次综合浏览量/秒。尝试了kgibbon的解决方案,但对我们没有用。

答案 1 :(得分:0)

解决方案:将testConnectionOnCheckout = true和testConnectionOnCheckin = true添加到c3p0 config。