The Java – c3p0 connection pool did not close the connection

I have one using c3p0 0.9 1.2,hibernate 3.2. 1. GA and spring 2.5 5. The problem is that the database connection is not closed by itself This is the log:

This is the data source 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>

This is our general 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);
}
}

Thank you in advance

Solution

Normally, hibernation automatically closes the connection However, there are several points to note:

>Long running transactions may occupy the connection > incorrect session management may mean that you will not close the session, which in turn means that the connection is still in use

A typical setup when using spring is to annotate your service methods with @ transactional This will manage your transactions and conversations

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>