简体   繁体   English

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

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

Still no luck with C3P0 pooling in Hibernate using Spring WebFlow if I use the following database.xml file and run my project I check the database and I only see two connects to it but I have it set to ten. 如果我使用下面的database.xml文件并运行我的项目,则使用Spring WebFlow在Hibernate中使用C3P0池仍然没有运气,我检查了数据库,但只看到两个连接,但将其设置为十。

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">
        <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>

But if I use this database.xml file I will see ten connects to the database but I want hibernate to manager the pool so can someone please tell me why the above database.xml does not work but the one below does? 但是,如果我使用此database.xml文件,我会看到有十个连接到数据库,但是我想休眠以管理池,所以有人可以告诉我为什么上面的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>

Why do you want hibernate to manage the pool? 为什么要让休眠管理池? The way it is setup in the second case is the correct way to configure dataSource & hibernate session factory with spring. 在第二种情况下,它的设置方式是使用spring配置dataSource和hibernate会话工厂的正确方法。 This will work correctly with spring transaction management. 这将在Spring事务管理中正常工作。

When you pass the dataSource as a parameter to the sessionFactoryBean the hibernate properties related to the dataSource configuration will be ignored. 当将dataSource作为参数传递给sessionFactoryBean时,与dataSource配置相关的休眠属性将被忽略。 If you insist upon hibernate managing the pool you can try by removing the dataSource property injection in the AnnotationSessionFactoryBean. 如果您坚持要休眠管理池,则可以尝试删除AnnotationSessionFactoryBean中的dataSource属性注入。 The driverClass, jdbcURL etc needs to now be specified as a part of hibernate properties. 现在需要将driverClass,jdbcURL等指定为休眠属性的一部分。

From the spring javadoc. 从Spring javadoc开始。

Set the DataSource to be used by the SessionFactory. 设置要由SessionFactory使用的数据源。 If set, this will override corresponding settings in Hibernate properties. 如果设置,它将覆盖休眠属性中的相应设置。 If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration. 如果设置了该设置,则休眠设置不应定义连接提供程序,以避免无意义的双重配置。

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

You can either let spring manage the connection pool (by way of configuring the datasource bean and passing it to sessionFactory) or let hibernate manage it completely. 您可以让spring管理连接池(通过配置数据源bean并将其传递给sessionFactory),也可以让hibernate对其进行完全管理。 Half & half will not work. 一半和一半将不起作用。

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

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