使用Spring在Hibernate中配置内置的c3p0池 [英] Configuring the built-in c3p0 pooling in Hibernate using Spring

查看:87
本文介绍了使用Spring在Hibernate中配置内置的c3p0池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < property name =hibernate.c3p0.min_size> 2< / property> 
< property name =hibernate.c3p0.max_size> 5< / property>
< property name =hibernate.c3p0.timeout> 600< / property>
< property name =hibernate.c3p0.max_statements> 0< / property>
< property name =hibernate.c3p0.idle_test_period> 300< / property>
< property name =hibernate.c3p0.acquire_increment> 1< / property>

然而我使用Spring配置Hibernate。当我试图在下面做时,它不起作用:

 < bean id =dataSourceclass =org。 apache.commons.dbcp.BasicDataSource> 
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost / news_loader/>
< property name =usernamevalue =blah/>
< property name =passwordvalue =blah/>

< property name =hibernate.c3p0.min_sizevalue =2/>
< property name =hibernate.c3p0.max_sizevalue =5/>
< property name =hibernate.c3p0.timeoutvalue =600/>
< property name =hibernate.c3p0.max_statementsvalue =0/>
< property name =hibernate.c3p0.idle_test_periodvalue =300/>
< property name =hibernate.c3p0.acquire_incrementvalue =1/>
< / bean>

我已阅读过有关使用独立c3p0池的信息,该池可以使用Spring进行配置,但是有什么方法可以在Spring中使用Spring配置内置c3p0池?



启发我,因为我是初学者。

解决方案

下面是一个示例配置(来自我们的应用程序),介绍如何在数据源中配置c3p0:

 < bean id =dataSourceGlobalclass =com.mchange.v2.c3p0.ComboPooledDataSource
destroy-method =close>
< property name =driverClassvalue =$ {driver}/>
< property name =jdbcUrlvalue =$ {server}/>
< property name =uservalue =$ {user}/>
< property name =passwordvalue =$ {passw}/>

<! - 这些是C3P0属性 - >
< property name =acquireIncrementvalue =$ {acquireIncrement}/>
< property name =minPoolSizevalue =$ {minPoolSize}/>
< property name =maxPoolSizevalue =$ {maxPoolSize}/>
< property name =maxIdleTimevalue =$ {maxIdleTime}/>
< / bean>

我们使用外部属性文件来配置一些参数,但它们也可以在Spring中直接配置。



如果您希望hibernate负责管理池,那么您需要配置会话属性:

 < bean id =sessionFactoryclass =org.springframework.orm.hibernate.LocalSessionFactoryBean> 
<! - 抑制InjectionValueTypeInspection - >
< property name =mappingResourcesref =hibernateMappingList/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> net.sf.hibernate.dialect.Oracle9Dialect< / prop>
< prop key =transaction.factory_class>
net.sf.hibernate.transaction.JDBCTransactionFactory
< / prop>
< prop key =hibernate.transaction.factory_class>
net.sf.hibernate.transaction.JDBCTransactionFactory
< / prop>
< prop key =hibernate.show_sql> false< / prop>
< prop key =hibernate.cglib.use_reflection_optimizer> false< / prop>
< prop key =hibernate.jdbc.batch_size> 0< / prop>

< prop name =hibernate.c3p0.min_sizevalue =2/>
< prop name =hibernate.c3p0.max_sizevalue =5/>
< prop name =hibernate.c3p0.timeoutvalue =600/>
< prop name =hibernate.c3p0.max_statementsvalue =0/>
< prop name =hibernate.c3p0.idle_test_periodvalue =300/>
< prop name =hibernate.c3p0.acquire_incrementvalue =1/>
< /道具>
< / property>
< / bean>

您必须使用以下其中一种方法:在数据源池或在休眠会话池中。切勿同时使用两者,因为这会浪费资源。


I learned that to configure c3p0 pooling in hibernate, we can have write the configuration in hibernate.cfg.xml such this:

<property name="hibernate.c3p0.min_size">2</property>
    <property name="hibernate.c3p0.max_size">5</property>
    <property name="hibernate.c3p0.timeout">600</property>
    <property name="hibernate.c3p0.max_statements">0</property>
    <property name="hibernate.c3p0.idle_test_period">300</property>
    <property name="hibernate.c3p0.acquire_increment">1</property>

However I configured Hibernate using Spring. When I tried to do below, it wouldn't work:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/news_loader" />
    <property name="username" value="blah" />
    <property name="password" value="blah" /> 

    <property name="hibernate.c3p0.min_size" value="2" />
    <property name="hibernate.c3p0.max_size" value="5" />
    <property name="hibernate.c3p0.timeout" value="600" />
    <property name="hibernate.c3p0.max_statements" value="0" />
    <property name="hibernate.c3p0.idle_test_period" value="300"/>
    <property name="hibernate.c3p0.acquire_increment" value="1" />
</bean>

I've read about using the stand-alone c3p0 pooling which can be configured using Spring, but is there any way that I can configure the built-in c3p0 pooling in Hibernate using Spring?

Enlighten me coz i'm a beginner.

解决方案

Here is a sample configuration (from our application) on how to configure c3p0 in the datasource:

<bean id="dataSourceGlobal" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="${driver}" />
        <property name="jdbcUrl" value="${server}" />
        <property name="user" value="${user}" />
        <property name="password" value="${passw}" /> 

        <!-- these are C3P0 properties -->
        <property name="acquireIncrement" value="${acquireIncrement}" />
        <property name="minPoolSize" value="${minPoolSize}" />
        <property name="maxPoolSize" value="${maxPoolSize}" />
        <property name="maxIdleTime" value="${maxIdleTime}" />
</bean>

We use an external property file to configure some parameters, but they can be configured directly in Spring too.

If you want hibernate to take care of the pooling, then you need to configure the Session properties:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <!--suppress InjectionValueTypeInspection -->
    <property name="mappingResources" ref="hibernateMappingList" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
            <prop key="transaction.factory_class">
                net.sf.hibernate.transaction.JDBCTransactionFactory
            </prop>
            <prop key="hibernate.transaction.factory_class">
                net.sf.hibernate.transaction.JDBCTransactionFactory
            </prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
            <prop key="hibernate.jdbc.batch_size">0</prop>

            <prop name="hibernate.c3p0.min_size" value="2" />
            <prop name="hibernate.c3p0.max_size" value="5" />
            <prop name="hibernate.c3p0.timeout" value="600" />
            <prop name="hibernate.c3p0.max_statements" value="0" />
            <prop name="hibernate.c3p0.idle_test_period" value="300"/>
            <prop name="hibernate.c3p0.acquire_increment" value="1" />
      </props>
    </property>
</bean>

You must use one of the approaches: either pool at the datasource or pool at the hibernate Session. Never use both, as it wastes resources.

这篇关于使用Spring在Hibernate中配置内置的c3p0池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆