有人可以帮我解决这个问题吗?
我有一个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 classess
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)
我应该如何以及在何处纠正近距离打开连接?
感谢您浪费时间陪伴我。
答案 0 :(得分:0)
我已阅读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可能会导致内存泄漏。
P.S。我还读到,Hibernate应该在会话提交或回滚时自动关闭连接,并且我不需要明确地关闭连接。
感谢您的建议,因为我总是需要有人睁大眼睛。