使用 Spring 在 Hibernate 中配置内置 c3p0 池

发布于 2024-10-30 16:22:42 字数 1485 浏览 3 评论 0原文

我了解到,要在 hibernate 中配置 c3p0 池,我们可以在 hibernate.cfg.xml 中编写配置,如下所示:

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

我已经阅读了有关使用可以使用 Spring 配置的独立 c3p0 池的信息,但是有什么方法可以使用在 Hibernate 中配置内置 c3p0 池春天?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捂风挽笑 2024-11-06 16:22:42

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

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

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

如果您希望 hibernate 负责池化,那么您需要配置 Session 属性:

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

您必须使用以下方法之一:数据源池或 hibernate Session 池。切勿同时使用两者,因为这会浪费资源。

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.

っ左 2024-11-06 16:22:42

在 spring 配置中,您使用 dbcp 而不是 cp30。 Spring正在创建数据源/连接池的实例。要为 dbcp 配置类似的参数,请直接设置它们的属性

<property name="maxActive" value="5"/>
<property name="minIdle" value="2"/>

等。您可以通过查看 javaodoc 来了解可用的属性 BasicDataSource 或从配置页面 http://commons.apache.org/dbcp/configuration.html

With the spring configuration you are using dbcp instead of cp30. Spring is creating an instance of the data source / connection pool. To configure simillar parameters for dbcp set them has properties directly

<property name="maxActive" value="5"/>
<property name="minIdle" value="2"/>

etc. You can find out about the properties available by looking at the javaodoc for BasicDataSource or from the configuration page http://commons.apache.org/dbcp/configuration.html.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文