简体   繁体   English

在配置文件中的何处放置c3p0属性?

[英]Where to place c3p0 properties in configuration file?

I am using spring/hibernate integrated application. 我正在使用spring/hibernate集成应用程序。 i have configured c3p0 connection pooling. 我已经配置了c3p0连接池。 the problem is if i set c3p0 properties in hibernate properties section then those properties are not considered and default configuration is taken. 问题是,如果我在hibernate属性部分设置c3p0属性,则不考虑这些属性,并采用默认配置。 if i set the same properties for combopooled datasource then they are considered. 如果我为组合池数据源设置相同的属性,则将其考虑在内。 where is the best place to place c3p0 properties. 在哪里放置c3p0属性的最佳位置。

Below configuration works: 以下配置有效:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driverClassName}" />
        <property name="jdbcUrl" value="${db.url}" />
        <property name="user" value="${db.username}" />
        <property name="password" value="${db.password}" />

        <!-- c3p0  properties -->

        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxStatements" value="0" />
        <property name="preferredTestQuery" value="select * from sometable" />
        </bean>

    <bean name="wygSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations" value="classpath:hibernate/module/*.hbm.xml" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.connection.pool.size">20</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                 <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>

                        </props>
        </property>
    </bean>

This does not work: 这不起作用:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driverClassName}" />
        <property name="jdbcUrl" value="${db.url}" />
        <property name="user" value="${db.username}" />
        <property name="password" value="${db.password}" />

                </bean>

    <bean name="wygSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations" value="classpath:hibernate/module/*.hbm.xml" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.connection.pool.size">20</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                 <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
<!-- c3p0  properties -->
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">0</prop>
                <prop key="hibernate.c3p0.preferredTestQuery">select * from sometable</prop>

                        </props>
        </property>
    </bean>

I was getting the same problem and it took time to figure out the solution. 我遇到了同样的问题,花了一些时间来找出解决方案。

I use Hibernate 4.0.1 and mysql 5.1(no spring framework) and I was facing the issue. 我使用的是Hibernate 4.0.1和mysql 5.1(没有spring框架),我正面临这个问题。 First make sure that you configured the c3p0 jars properly which are essential. 首先,请确保已正确配置了c3p0 jar。

I used these properties in hibernate.cfg.xml 我在hibernate.cfg.xml中使用了这些属性

<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<property name="hibernate.c3p0.idle_test_period">10</property>
<property name="hibernate.c3p0.acquireRetryAttempts">5</property>
<property name="hibernate.c3p0.acquireRetryDelay">200</property>
<property name="hibernate.c3p0.timeout">40</property>

But it's of no use 'cause C3p0 was still taking the default properties not the properties which I set in hibernate.cfg.xml, You can check it in logs. 但这没有用,因为C3p0仍采用默认属性,而不是我在hibernate.cfg.xml中设置的属性,您可以在日志中检查它。 So, I searched many websites for right solution and finally I came up with this. 因此,我在许多网站上搜索了正确的解决方案,最后我想到了这个解决方案。 remove the C3p0 properties in cfg.xml and create c3p0-config.xml in the root path(along with cfg.xml) and set properties as follows. 删除cfg.xml中的C3p0属性,并在根路径(以及cfg.xml)中创建c3p0-config.xml并按如下所示设置属性。

<c3p0-config>
<default-config> 
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">40</property> 
<property name="idleConnectionTestPeriod">10</property> 
<property name="initialPoolSize">10</property>
<property name="maxPoolSize">20</property> 
<property name="minPoolSize">5</property> 
<property name="maxStatements">50</property>
<property name="preferredTestQuery">SELECT 1;</property>
<property name="acquireRetryAttempts">5</property>
<property name="acquireRetryDelay">200</property>
<property name="maxIdleTime">30</property>
</default-config>
</c3p0-config>

but if you run, ORM takes the jdbc connection but not C3p0 connection pool 'cause we should add these properties in hibernate.cfg.xml 但是如果运行,ORM将使用jdbc连接,而不使用C3p0连接池,因为我们应该在hibernate.cfg.xml中添加这些属性

<property name="hibernate.c3p0.validate">true</property>

<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>

now everything works fine(At least it worked fine for me) and the issue is solved. 现在一切正常(至少对我来说正常),问题已解决。

check the following for references. 检查以下内容以供参考。

http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing

https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool

I hope this solves your problem. 我希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM