2012-09-07 26 views
0

如果我使用以下database.xml文件並運行我的項目,仍然沒有運用C3P0在Hibernate中使用Spring WebFlow彙集我檢查數據庫,並且我只看到兩個連接到它的數據庫但我已將它設定爲十。仍然沒有運用C3P0在Hibernate中使用Spring WebFlow合併

database.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
          http://www.springframework.org/schema/jdbc 
          http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"> 

    <context:property-placeholder location="classpath:jdbc.properties" /> 
    <context:component-scan base-package="org.uftwf" /> 
    <tx:annotation-driven transaction-manager="hibernateTransactionManager" /> 

    <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}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>org.uftwf.schoolvisit.model.VisitModel</value> 
       <value>org.uftwf.schoolvisit.model.NameID_lookupModel</value> 
       <value>org.uftwf.schoolvisit.model.School_lookupModel</value> 
      </list> 
     </property> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop> 
       <prop key="format_sql">${format_sql}</prop> 
       <prop key="hibernate.c3p0.min_size">10</prop> 
       <prop key="hibernate.c3p0.max_size">25</prop> 
       <prop key="hibernate.c3p0.timeout">600</prop> 
       <prop key="hibernate.c3p0.max_statements">0</prop> 
       <prop key="hibernate.c3p0.idle_test_period">300</prop> 
       <prop key="hibernate.c3p0.acquire_increment">5</prop> 
      </props> 
     </property> 
    </bean> 

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

但是,如果我用這個database.xml文件我十歲了連接到數據庫,但我想Hibernate來管理池中,以便有人可以告訴我爲什麼上面的數據庫。 XML不起作用,但下面的一個呢?

database.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
          http://www.springframework.org/schema/jdbc 
          http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"> 

    <context:property-placeholder location="classpath:jdbc.properties" /> 
    <context:component-scan base-package="org.uftwf" /> 
    <tx:annotation-driven transaction-manager="hibernateTransactionManager" /> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
     destroy-method="close"> 

     <!-- these are C3P0 properties --> 
     <property name="acquireIncrement" value="${database.acquireIncrement}" /> 
     <property name="minPoolSize" value="${database.minPoolSize}" /> 
     <property name="maxPoolSize" value="${database.maxPoolSize}" /> 
     <property name="maxIdleTime" value="${database.maxIdleTime}" /> 
     <property name="idleConnectionTestPeriod" value="300" /> 

     <property name="driverClass" value="${database.driver}" /> 
     <property name="jdbcUrl" value="${database.url}" /> 
     <property name="user" value="${database.user}" /> 
     <property name="password" value="${database.password}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>org.uftwf.schoolvisit.model.VisitModel</value> 
       <value>org.uftwf.schoolvisit.model.NameID_lookupModel</value> 
       <value>org.uftwf.schoolvisit.model.School_lookupModel</value> 
      </list> 
     </property> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop> 
       <prop key="format_sql">${format_sql}</prop> 
       <prop key="hibernate.c3p0.min_size">10</prop> 
       <prop key="hibernate.c3p0.max_size">25</prop> 
       <prop key="hibernate.c3p0.timeout">600</prop> 
       <prop key="hibernate.c3p0.max_statements">0</prop> 
       <prop key="hibernate.c3p0.idle_test_period">300</prop> 
       <prop key="hibernate.c3p0.acquire_increment">5</prop> 
      </props> 
     </property> 
    </bean> 

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

回答

1

爲什麼你要冬眠管理池?在第二種情況下設置的方式是使用spring配置hibernate session factory的數據源&的正確方法。這將在spring事務管理中正常工作。

當您將dataSource作爲參數傳遞給sessionFactoryBean時,與數據源配置相關的hibernate屬性將被忽略。如果你堅持使用Hibernate來管理池,你可以通過移除AnnotationSessionFactoryBean中的dataSource屬性注入來嘗試。現在需要將driverClass,jdbcURL等指定爲hibernate屬性的一部分。

從春天javadoc。

設置由SessionFactory使用的數據源。如果設置了,這將會覆蓋Hibernate屬性中的相應設置 。如果設置爲 ,則Hibernate設置不應將連接提供者定義爲 ,以避免無意義的雙重配置。

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/orm/hibernate3/AbstractSessionFactoryBean.html#setDataSource(javax.sql.DataSource

您可以讓Spring來管理連接池(通過配置數據源的bean並將它傳遞給SessionFactory的方式),或者讓Hibernate完全管理。一半&一半將無法正常工作。

+0

如果我們使用HibernateDaoSupport類,會自動彈出管理連接池嗎?如果是的話,最小和最大連接是什麼? – jaleel

相關問題