2014-07-10 108 views
1

我們使用了Spring,Hibernate和C3P0連接池。但即使我們設置了maxIdleTime = 3,maxConnectionAge = 3,maxIdleTimeExcessConnections = 1,但連接並未關閉。Spring,Hibernate C3P0連接池,物理連接未關閉

以下是調度-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實現代碼

服務FPGA實現類

@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(); 


    } 

}  

在此先感謝。在調試/跟蹤

+0

你怎麼會知道,連接不關閉? – Amogh

+0

您可以在** Microsoft SQL Server Management Studio **中看到打開的連接。要查看數據庫連接,只需打開** Microsoft SQL Server Management Studio **,然後展開**管理**,然後雙擊**活動監視器**,您將看到數據庫的打開連接。 – Sushil

+0

什麼是您的SQL服務器等待超時。你確定只設置'maxIdleTime'只需3秒? – Amogh

回答

0

我們使用的調度-servlet.xml中文件以下行,現在數據庫的物理連接現在關閉。
< TX:註解驅動的代理目標類=「真正的」交易經理=「transactionManager的」/>

+0

爲什麼它解決了這個問題? – DerekY

0

認沽包com.mchange.v2.resourcepool在log4j的配置記錄器,你應該看到這樣的事情

DEBUG (BasicResourcePool.java:1458) 16:17:27,943 BasicResourcePool - Removing expired resource: [email protected] [[email protected]] 

DEBUG (BasicResourcePool.java:962) 16:17:27,944 BasicResourcePool - Preparing to destroy resource: [email protected] 

DEBUG (BasicResourcePool.java:967) 16:17:27,946 BasicResourcePool - Successfully destroyed resource: [email protected] 

但是我所觀察到的是,一旦所有的連接被破壞,在此了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