【问题标题】:Postgresql + Hibernate3 + C3p0 = not closing connectionsPostgresql + Hibernate3 + C3p0 = 不关闭连接
【发布时间】:2011-10-23 19:14:37
【问题描述】:

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

休眠异常:

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

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

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

连接

这是我的休眠 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>    

编辑----

这是我为获取和关闭会话而调用的代码。

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

}

【问题讨论】:

  • 更有趣的是你如何检索和释放会话

标签: hibernate postgresql c3p0


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我们遇到了同样的问题并切换到 BoneCP。现在一切都运行良好。我们有一个负载很高的网站,峰值为 3000 次浏览量/秒。 kgibbon 的解决方案已尝试但对我们不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      相关资源
      最近更新 更多