gpt4 book ai didi

java - 似乎 Hibernate 超出了连接限制

转载 作者:行者123 更新时间:2023-11-28 23:34:58 28 4
gpt4 key购买 nike

谁能帮我解决这个问题?我有一个 Tomcat 和简单的 JSF 应用程序:https://github.com/gooamoko/jsfbilling/ .当我在 Tomcat 上运行应用程序时,它运行正常,但在多次请求(例如 10 次快速刷新页面)后引发异常 can't open connection。我想,这很正常,但哪里错了?

在配置文件hibernate.cfg.xml中

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/netstat</property>
<property name="connection.username">netstat</property>
<property name="connection.password">netstat</property>

<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- configuration pool via c3p0 -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">200</property>
<property name="c3p0.timeout">600</property> <!-- seconds -->

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping classes -->
<mapping class="ru.gooamoko.model.Group" />
<mapping class="ru.gooamoko.model.Host" />
<mapping class="ru.gooamoko.model.Traffic" />
<mapping class="ru.gooamoko.model.DailyTraffic" />

</session-factory>
</hibernate-configuration>

或者在 Java 类中

package ru.gooamoko.dao;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateProvider {
private static SessionFactory factory;
private static ServiceRegistry registry;

public static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
registry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
factory = configuration.buildSessionFactory(registry);
return factory;
}

}


package ru.gooamoko.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class GenericDao {

private SessionFactory factory;
protected Session session;

protected void begin() {
session = factory.getCurrentSession();
session.beginTransaction();
}

protected void commit() {
if (session.isOpen()) {
session.getTransaction().commit();
}
}

protected void rollback() {
if (session.isOpen()) {
session.getTransaction().rollback();
}
}

public GenericDao() {
this.factory = HibernateProvider.getSessionFactory();
}
}

在 tomcat 日志中我看到了这个

27-Aug-2014 15:06:24.559 WARNING [C3P0PooledConnectionPoolManager[identityToken->1hge12w9467h4hm1tfa5tj|3b40a97d]-HelperThread-#2] com.mchange.v2.resourcepool.BasicResourcePool.forceKillAcquires Having failed to acquire a resource, com.mchange.v2.resourcepool.BasicResourcePool@4df5a3a4 is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.
27-Aug-2014 15:06:24.563 WARNING [C3P0PooledConnectionPoolManager[identityToken->1hge12w9467h4hm1tfa5tj|3b40a97d]-HelperThread-#2] com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@628977a2 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
org.postgresql.util.PSQLException: ?????: ?????????? ????? ??????????? ??????????????? ??? ??????????? ????????????????? (?? ??? ??????????)
at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:572)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:177)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:195)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:184)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)

我应该如何以及在何处更正关闭打开的连接?

谢谢你为我浪费你的时间。

最佳答案

我阅读了 Hibernate 文档并找到了 HibernateUtil 类的示例。在与我的 HibernateProvider 进行比较后,我发现,在我的 HibernateProvider 工厂中似乎是在每次调用 getSessionFactory() 时构建的。 HibernateProvider 的新版本是

package ru.gooamoko.dao;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateProvider {

private static final SessionFactory factory = buildFactory();

public static SessionFactory getSessionFactory() {
return factory;
}

private static SessionFactory buildFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
return configuration.buildSessionFactory(registry);
} catch (HibernateException ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}

那么,现在查询

 SELECT COUNT(*) FROm pg_stat_activity;

返回6,其中一个是psql客户端。即使在重新请求页面一分钟之后。我认为,这是一个进步。

剩下的唯一一件事是让 Hibernate 可以与 Tomcat/lib 文件夹中的 postgresql-jdbc.jat 一起工作,而不使用 WEB-INF/lib 中的 jar。我读到将 postgresql-jdbc.jar 放入 WEB-INF/lib 可能会导致内存泄漏。

附言我还读到,Hibernate 应该在 session 提交或回滚时自动关闭连接,我不需要显式关闭连接。

感谢您的建议,因为我总是需要一个让我睁大眼睛的人。

关于java - 似乎 Hibernate 超出了连接限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25522245/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com