【问题标题】:Trying to setup pooling with C3P0 Pooling in Hibernate with Spring WebFlow and MySQL尝试使用 Spring WebFlow 和 MySQL 在 Hibernate 中使用 C3P0 池设置池
【发布时间】:2012-08-31 15:31:03
【问题描述】:

我正在尝试使用 Spring WebFlow 和 MySQL 在 Hibernate 中使用 C3P0 Pooling 设置池,但我想让 Hibernate 管理池而不是 mysql,所以我设置我的 jdbc.properties 文件和我的 database.xml 文件并运行我的项目并检查了与数据库的连接,看起来如果我在 MySQL 端更改 database.minPoolSize 属性,它可以工作,但如果我在 Hibernate 大小上更改它们,它就不起作用。有人可以查看我的文件并告诉我您的想法吗:

jdbc.properties:

#
# database
#
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://xxx
database.user=xxx
database.password=xxx
database.acquireIncrement=1
database.minPoolSize=5
database.maxPoolSize=25
database.maxIdleTime=1
#
# Hibernate
#
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql=false
format_sql=false
hibernate.use_sql_comments=false;
#
# C3P0
#
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=25
hibernate.c3p0.timeout=600
hibernate.c3p0.max_statements=0
hibernate.c3p0.idle_test_period=300
hibernate.c3p0.acquire_increment=5

这是我的 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="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="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
        destroy-method="close"> <property name="driverClassName" value="${database.driver}" 
        /> <property name="url" value="${database.url}" /> <property name="username" 
        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>

为什么C3P0不使用hibernate属性而是使用mysql onces?

【问题讨论】:

    标签: java spring hibernate spring-webflow c3p0


    【解决方案1】:

    您正在使用普通属性定义您的 C3P0 bean。将 ${database...} 更改为您想要的关联键。

    【讨论】:

    • 是的,但是文件中的那些值是 MySQL 特定的值,而不是 Hibernate 的值。例如,您已将 hibernate.c3p0.max_size 的 bean 属性硬编码为 25,并使用 MySQL 属性。你应该通过使用那些hibernate.* 属性来定义DataSource 来简化你自己的生活,以避免通过混合它们来避免维护的噩梦。这将有助于避免这样的混淆。并且不要对具有数据属性的值进行硬编码。
    • 能否请您发布一个关于如何使用hirernate.*作为数据源的示例
    • &lt;property name="minPoolSize" value="${database.minPoolSize}" /&gt; 更改为 &lt;property name="minPoolSize" value="${hibernate.c3p0.min_size}" /&gt;。本质上,分享价值观。定义属性两次,特别是当您甚至在 Spring 文件中将它们表示为匹配时,这只是在要求这种类型的混淆。
    猜你喜欢
    • 2012-08-31
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多