繁体   English   中英

使用Spring WebFlow在Hibernate中使用C3P0进行池化仍然没有运气

[英]Still no luck with C3P0 pooling in Hibernate using Spring WebFlow

如果我使用下面的database.xml文件并运行我的项目,则使用Spring WebFlow在Hibernate中使用C3P0池仍然没有运气,我检查了数据库,但只看到两个连接,但将其设置为十。

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文件,我会看到有十个连接到数据库,但是我想休眠以管理池,所以有人可以告诉我为什么上面的database.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>

为什么要让休眠管理池? 在第二种情况下,它的设置方式是使用spring配置dataSource和hibernate会话工厂的正确方法。 这将在Spring事务管理中正常工作。

当将dataSource作为参数传递给sessionFactoryBean时,与dataSource配置相关的休眠属性将被忽略。 如果您坚持要休眠管理池,则可以尝试删除AnnotationSessionFactoryBean中的dataSource属性注入。 现在需要将driverClass,jdbcURL等指定为休眠属性的一部分。

从Spring javadoc开始。

设置要由SessionFactory使用的数据源。 如果设置,它将覆盖休眠属性中的相应设置。 如果设置了该设置,则休眠设置不应定义连接提供程序,以避免无意义的双重配置。

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

您可以让spring管理连接池(通过配置数据源bean并将其传递给sessionFactory),也可以让hibernate对其进行完全管理。 一半和一半将不起作用。

暂无
暂无

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

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