Spring,Hibernate C3P0连接池,物理连接未关闭

时间:2014-07-10 13:04:49

标签: spring hibernate connection-pooling

我们使用了Spring,Hibernate和C3P0连接池。但即使我们设置 maxIdleTime = 3 maxConnectionAge = 3 maxIdleTimeExcessConnections = 1 ,连接也不会关闭。

以下是 dispatcher-servlet.xml 代码

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="jdbcUrl" value="${jdbcUrl}"/>
        <property name="user" value="${user}"/>
        <property name="password" value="${password}"/>
        <property name="maxPoolSize" value="10"/>
        <property name="initialPoolSize" value="2"/>
        <property name="acquireIncrement"  value="1"/>          
        <property name="minPoolSize" value="1"/>
        <property name="maxIdleTime" value="3"/>
        <property name="maxConnectionAge" value="3"/>
        <property name="maxIdleTimeExcessConnections" value="1"/>        
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="ConfigurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            </props>  
        </property>         
    </bean>         

    <tx:annotation-driven/>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <mvc:annotation-driven/>

以下是服务 DAO 实施代码

服务实施类

@Service
public class SaveFeedbackServiceImpl implements SaveFeedbackService 
{ 

    @Autowired
    private SaveFeedBackDAO saveFeedbackDAO;


    @Transactional
    public void saveStudentTestFeedback(FeedbackMaster fm,String akey,int testid)
    {
        saveFeedbackDAO.saveStudentTestFeedback(fm, akey, testid);
    }
}

DAO实施类

@Repository
public class SaveFeedBackImpl implements SaveFeedBackDAO
{

        @Autowired
    private SessionFactory sessionFactory; 

    @Override
    public void saveStudentTestFeedback(FeedbackMaster fm,String akey,int testid)
    {


        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(FeedbackMaster.class);
        criteria.add(Restrictions.eq("accesskey", akey)).add(Restrictions.eq("testid", testid));

        FeedbackMaster fdkm = (FeedbackMaster) criteria.uniqueResult();

        fdkm.setFbq1(fm.getFbq1());
        fdkm.setFbq2(fm.getFbq2());
        fdkm.setFbq3(fm.getFbq3());
        fdkm.setFbq4(fm.getFbq4());
        fdkm.setFbq5(fm.getFbq5());
        fdkm.setFbq6(fm.getFbq6());
        fdkm.setFbq7(fm.getFbq7());
        fdkm.setFbq8(fm.getFbq8());
        fdkm.setFbq9(fm.getFbq9());
        fdkm.setFbq10(fm.getFbq10());


        sessionFactory.getCurrentSession().update(fdkm);


        String hql = " UPDATE StudentTestDetail set testStatus=:testStatus "+
                     " WHERE accesskey = :akey and testid=:testid ";

         Query query = sessionFactory.getCurrentSession().createQuery(hql);
         query.setParameter("testStatus",4);
         query.setParameter("akey",akey);
         query.setParameter("testid",testid);        
         query.executeUpdate();


    }

}    

先谢谢。

2 个答案:

答案 0 :(得分:0)

通过在log4j中配置logger将软件包com.mchange.v2.resourcepool置于调试/跟踪上,您应该会看到类似这样的内容

DEBUG (BasicResourcePool.java:1458) 16:17:27,943 BasicResourcePool - Removing expired resource: com.mchange.v2.c3p0.impl.NewPooledConnection@45e9ddca [com.mchange.v2.resourcepool.BasicResourcePool@75b5b0c7]

DEBUG (BasicResourcePool.java:962) 16:17:27,944 BasicResourcePool - Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@45e9ddca

DEBUG (BasicResourcePool.java:967) 16:17:27,946 BasicResourcePool - Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@45e9ddca

然而,我观察到的是,一旦所有连接都被销毁,将再次创建minPoolSize(此测试运行中的15个)连接数。在我的工作中我不使用这个池但是我只是猜测池的基本思想是准备连接,所以即使所有都是空闲的并且所有都需要物理关闭,所以池是预期的拥有最少数量的池,以便重新创建它们。

DEBUG (BasicResourcePool.java:404) 16:18:31,019 BasicResourcePool - incremented pending_acquires: 1
DEBUG (BasicResourcePool.java:404) 16:18:31,020 BasicResourcePool - incremented pending_acquires: 2
DEBUG (BasicResourcePool.java:404) 16:18:31,020 BasicResourcePool - incremented pending_acquires: 3
DEBUG (BasicResourcePool.java:404) 16:18:31,020 BasicResourcePool - incremented pending_acquires: 4
DEBUG (BasicResourcePool.java:404) 16:18:31,021 BasicResourcePool - incremented pending_acquires: 5
DEBUG (BasicResourcePool.java:404) 16:18:31,021 BasicResourcePool - incremented pending_acquires: 6
DEBUG (BasicResourcePool.java:404) 16:18:31,021 BasicResourcePool - incremented pending_acquires: 7
DEBUG (BasicResourcePool.java:404) 16:18:31,022 BasicResourcePool - incremented pending_acquires: 8
DEBUG (BasicResourcePool.java:404) 16:18:31,022 BasicResourcePool - incremented pending_acquires: 9
DEBUG (BasicResourcePool.java:404) 16:18:31,022 BasicResourcePool - incremented pending_acquires: 10
DEBUG (BasicResourcePool.java:404) 16:18:31,022 BasicResourcePool - incremented pending_acquires: 11
DEBUG (BasicResourcePool.java:404) 16:18:31,023 BasicResourcePool - incremented pending_acquires: 12
DEBUG (BasicResourcePool.java:404) 16:18:31,023 BasicResourcePool - incremented pending_acquires: 13
DEBUG (BasicResourcePool.java:404) 16:18:31,023 BasicResourcePool - incremented pending_acquires: 14
DEBUG (BasicResourcePool.java:404) 16:18:31,027 BasicResourcePool - incremented pending_acquires: 15

答案 1 :(得分:0)

我们在 dispatcher-servlet.xml 文件中使用了以下行,现在数据库物理连接正在关闭。
&lt; tx:annotation-driven proxy-target -class =&#34;真&#34;事务管理器=&#34; transactionManager的&#34; /&GT;