您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

这是有关如何在数据源中配置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处理池,则需要配置会话属性

<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会话中池。切勿同时使用两者,因为这会浪费资源。

Java 2022/1/1 18:18:00 有477人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶