Java c3p0 连接池未关闭连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9824879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c3p0 Connection Pool not closing connections
提问by Oualid
I have a Using c3p0 0.9.1.2 , hibernate 3.2.1.ga and spring 2.5.5. The problem is the database connection doesn't close itself. Here are the logs :
我有一个 Using c3p0 0.9.1.2 ,hibernate 3.2.1.ga 和 spring 2.5.5。问题是数据库连接不会自行关闭。以下是日志:
[22 mars 2012 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] acquire test -- pool is already maxed out. [managed: 20; max: 20] [22 mars 2012 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] awaitAvailable(): com.mchange.v2.c3p0.impl.NewPooledConnection@15cc604 [22 mars 2012 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] trace com.mchange.v2.resourcepool.BasicResourcePool@6b0524 [managed: 20, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@15cc604)
[2012 年 3 月 22 日 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] 获取测试 -- 池已满。[管理:20;max: 20] [22 mars 2012 12:29:56,091] DEBUG com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] awaitAvailable(): com.mchange.v2.c3p0.impl .NewPooledConnection@15cc604 [2012 年 3 月 22 日 12:29:56,091] 调试 com.mchange.v2.resourcepool.BasicResourcePool ["http-apr-8080"-exec-4] 跟踪 [com.mchange.v2.resourcepool@6b0524]托管:20,未使用:0,排除:0](例如 com.mchange.v2.c3p0.impl.NewPooledConnection@15cc604)
Here's the datasource configuration :
这是数据源配置:
<!-- Local DataSource that works in any environment -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${database.driver}"/>
<property name="jdbcUrl" value="${database.url}"/>
<property name="user" value="${database.user}"/>
<property name="password" value="${database.password}"/>
<!--<property name="connectionCustomizerClassName" value="org.xxx.webapp.common.persistence.WatchConnectionCustomizer"/>-->
<property name="maxStatements" value="500"/>
<property name="maxIdleTime" value="1800"/>
<property name="maxPoolSize" value="100"/>
<property name="minPoolSize" value="2"/>
<property name="initialPoolSize" value="2"/>
<property name="acquireIncrement" value="3"/>
<property name="idleConnectionTestPeriod" value="3000"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>
<prop key="hibernate.transaction.auto_close_session">${hibernate.transaction.auto_close_session}</prop>
<prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
...
</list>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
Here's our generic Dao
这是我们的通用 Dao
public class GenericDAO<T, PK extends Serializable> extends HibernateDaoSupport
implements IGenericDAO<T, PK> {
private Class<T> clazz;
private Logger logger = Logger.getLogger(GenericDAO.class);
private static Session session;
public GenericDAO(Class<T> clazz) {
this.clazz = clazz;
}
public void refresh(T instanceToRefresh) throws DataAccessException {
getHibernateTemplate().refresh(instanceToRefresh);
//getCurrentSession().refresh(instanceToRefresh);
}
public void saveOrUpdate(T instanceToSaveOrUpdate)
throws DataAccessException {
//getCurrentSession().saveOrUpdate(instanceToSaveOrUpdate);
getHibernateTemplate().saveOrUpdate(instanceToSaveOrUpdate);
}
public void persist(T instanceToPersist) throws DataAccessException {
getHibernateTemplate().persist(instanceToPersist);
//getCurrentSession().persist(instanceToPersist);
}
@SuppressWarnings("unchecked")
public T merge(T instanceToMerge) throws DataAccessException {
T instanceMerged = (T) getHibernateTemplate().merge(instanceToMerge);
//T instanceMerged = (T) getCurrentSession().merge(instanceToMerge);
return instanceMerged;
}
@SuppressWarnings("unchecked")
public PK save(T newInstance) throws DataAccessException {
return (PK) getHibernateTemplate().save(newInstance);
//return (PK) getCurrentSession().save(newInstance);
}
public void delete(T persistentObject) throws DataAccessException {
getHibernateTemplate().delete(persistentObject);
//getCurrentSession().delete(persistentObject);
}
@SuppressWarnings("unchecked")
public T load(PK id) {
return (T) getHibernateTemplate().get(clazz, id);
//return (T) getCurrentSession().get(clazz, id);
}
public void update(T transientObject) throws DataAccessException {
//getCurrentSession().update(transientObject);
getHibernateTemplate().update(transientObject);
}
@SuppressWarnings("unchecked")
public List<T> loadAll() throws DataAccessException {
//Session session = this.getCurrentSession();
//return session.createQuery("from " + clazz.getName()).list();
return getHibernateTemplate().loadAll(clazz);
}
}
Thanks in advance.
提前致谢。
采纳答案by Bozho
Normally, the connection is automatically closed by hibernate. However, a few things to note:
通常,连接会被休眠自动关闭。但是,有几点需要注意:
- long-running transactions may occupy a connection
- improper session management may mean you don't close your session, which in turn means the connection remains in use
- 长时间运行的事务可能会占用一个连接
- 不正确的会话管理可能意味着您没有关闭会话,这反过来意味着连接仍在使用中
The typical setup when using spring is to annotate your service methods with @Transactional
. That way spring will manage your transactions and sessions.
使用 spring 时的典型设置是使用@Transactional
. 这样 spring 将管理您的事务和会话。
回答by Sushil
We have used the following line in the dispatcher-servlet.xml
file and database physical connections are now closing.
我们在dispatcher-servlet.xml
文件和数据库物理连接中使用了以下行现在正在关闭。
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager"/>