Hibernate无法关闭Postgresql的连接 [英] Hibernate cannot close connection for Postgresql

查看:64
本文介绍了Hibernate无法关闭Postgresql的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用heroku远程PostgreSQL数据库,在我的应用程序中,我还使用了hibernate和c3p0.已经设置了最大连接,并在使用后关闭了连接,但是所有这些都无法关闭数据库中的空闲连接.我的代码中有错误?因为如果我只更改url并连接到本地MySQL,它就可以工作.所以我认为PostgreSQL很特别.还是因为heroku远程数据库限制?

I am using a heroku remote PostgreSQL database, and in my application, I also use hibernate and c3p0. Already set max connection, and close connection after use, but all of these failed to close idle connection in database. Errors in my code? Since if I only change url and connect to a local MySQL, it works. So I am thinking PostgreSQL is special. Or it is because the heroku remote db restriction?

hibernate.cfg.xml就是这样.我更改了它以适应本地mysql,并且它确实关闭了连接.

hibernate.cfg.xml is like this. I change it to adapt local mysql, and it does close connections.

<session-factory>

    <!-- hibernate connection configuration -->
    <property name="connection.url">jdbc:mysql://localhost/website</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>

    <!-- hibernate performance configuration -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
    <property name="show_sql">true</property>

    <!-- c3p0 configuration -->
    <property name="c3p0.acquire_increment">1</property>
    <property name="c3p0.idle_test_period">10</property>
    <property name="c3p0.max_statements">3</property>
    <property name="c3p0.max_size">2</property>
    <property name="c3p0.min_size">1</property>
    <property name="c3p0.timeout">90</property>
    <property name="transaction.auto_close_session">true</property>
    <property name="idleConnectionTestPeriod">60</property>
    <property name="maxIdleTime">30</property>





    <!-- presistance objects -->
    <mapping class="pojo.WebSiteCommentPOJO"/>

</session-factory>

HibernateUtil是这样的:

HibernateUtil is like this:

    public class HibernateUtils {

private static SessionFactory sessionFactory;

public static SessionFactory getSessionFactory() {
        if (sessionFactory != null) {
            return sessionFactory;
        }
    Configuration conf = new Configuration().configure();

    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();

    serviceRegistryBuilder.applySettings(conf.getProperties());

    /*ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();*/

    SessionFactory sf = conf.buildSessionFactory();
    return sf;
}

public static Session getSession() {
    return getSessionFactory().openSession();
}

}

下面的代码是我使用休眠方式的方法.

And the following code is how I use hibernate.

public WebSiteComment getSingle(int id) {
    Session ss = HibernateUtils.getSession();
    ss.beginTransaction();
    WebSiteCommentPOJO w = ss.get(WebSiteCommentPOJO.class,id);
    ss.getTransaction().commit();

    return new WebSiteComment(w.getId(), w.getComment(), w.getEmail(), w.getDate());
}

推荐答案

您需要关闭 session 才能关闭连接.提交事务后,会话和连接不会关闭,因为您可以开始新的事务或对会话执行其他操作.MySQL可能有大量可以打开的连接.当然,Heroku的连接数量有限.

You need to close the session to close a connection. When transaction is committed session and connection are not closed because of you can begin a new transaction or do something else with a session. Probable, MySQL have a large number of connections that can be opened. Heroku, of course, has a limited number of connections.

使用 finally 块以任何方式关闭 session .您可以使用

Use finally block to close the session in any way. You can use this pattern to work with a session.

只需执行此操作即可配置会话工厂

Just do this to configure a session factory

SessionFactory sf = new Configuration().configure().buildSessionFactory();

如果发生错误,您需要回滚事务.

And you need to rollback transaction in case of error.

更新

由于使用,我的答案可能不太正确

My answer, probably, is not very correct because of using

<属性名称="transaction.auto_close_session"> true</property>

这篇关于Hibernate无法关闭Postgresql的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆