应该在哪里指定c3p0属性?

时间:2012-05-11 14:55:02

标签: java spring hibernate c3p0

我正在使用spring / hibernate集成应用程序。我已经配置了c3p0连接池。我正在使用c3p0 combopooled数据源。我应该在哪里指定combopooled数据源属性?

在本节中?

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingLocations" value="classpath:hibernate-mapping.xml" />
        <property name="hibernateProperties">
            <props>
                **//Here do I need to specify combopooled datasource properties?
        //like acquireIncrement
            acquireRetryAttempts
            acquireRetryDelay
            preferredTestQuery
            maxPoolSize...etc**
            </props>
        </property>
        <property name="dataSource" ref="dataSource" />
    </bean>

或者在这里?

<bean id="rootDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
**//Here do i need to specify combopooled datasource properties?
            //like acquireIncrement
            acquireRetryAttempts
            acquireRetryDelay
            preferredTestQuery**
            maxPoolSize...etc

</bean>

请建议我。

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以在c3p0.properties文件中指定它们。只需将其放在类路径的根目录

即可

或者,在类路径根目录中名为c3p0-config.xml的xml文件中

或者,如果你想在它中使用Spring for xiber for Hibernate,你可以使用

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <!--<property name="lobHandler">
         <ref local="lobHandler" />
      </property>-->
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.connection.driver_class">${jdbc.drivers}</prop>
            <prop key="hibernate.connection.url">${jdbc.url}</prop>
            <prop key="hibernate.connection.username">${jdbc.username}</prop>
            <prop key="hibernate.connection.password">${jdbc.password}</prop>
            <prop key="hibernate.c3p0.min_size">5</prop>
            <prop key="hibernate.c3p0.max_size">100</prop>
            <prop key="hibernate.c3p0.timeout">1800</prop>
            <prop key="hibernate.c3p0.max_statements">50</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
         </props>
      </property>

我相信Hibernate设置会覆盖使用c3po.properties文件(如果两者都存在)。但是this post表明与c3po文档存在一些不一致

答案 1 :(得分:0)

那些应该继续使用数据源bean(rootDataSource)。一旦将数据源传递给LocalSessionFactoryBean,hibernate将不会创建连接池 - 因此将忽略hibernateProperties下指定的属性。

您要设置的所有属性均可在ComboPooledDataSource上作为setter方法使用。向数据源bean添加更多属性标记,并为其指定值。

<bean id="rootDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="password" value="${jdbc.password}" />
        <property name="acquireRetryAttempts" value="..." />
        <property name="acquireRetryDelay" value="..." />
        <property name="preferredTestQuery" value="..." />
        <property name="maxPoolSize" value="..." />    
    </bean>