【发布时间】:2016-06-07 17:09:35
【问题描述】:
我正在使用 Heroku 远程 PostgreSQL 数据库,在我的应用程序中,我还使用 hibernate 和 c3p0。已经设置了最大连接,使用后关闭连接,但所有这些都无法关闭数据库中的空闲连接。我的代码中的错误?因为如果我只更改 url 并连接到本地 MySQL,它就可以工作。所以我认为 PostgreSQL 很特别。还是因为heroku远程数据库的限制?
hibernate.cfg.xml 是这样的。我将其更改为适应本地mysql,它确实关闭了连接。
<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 是这样的:
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();
}
}
下面的代码是我如何使用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());
}
【问题讨论】:
标签: hibernate postgresql connection c3p0