1. 程式人生 > >c3p0在spring下的配置過程

c3p0在spring下的配置過程

前幾天在配置hibernate的c3p0連線池時報出了死鎖的異常。經過一頓的學習,現在專案中已經解決了這個問題,現將其配置記錄於下,供自己和有需要的人查閱。
1、在hibernate.properties中新增:
hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider

2、將c3p0的常規配置我新建在了自己寫的properites中,取名:jdbc.properties,關於c3p0的程式碼如下:
######C3P0 MySQL config #######
c3p0.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8
c3p0.user=root
c3p0.password=root

c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.acquireIncrement=1
c3p0.maxIdleTime=60
c3p0.maxPoolSize=200
c3p0.minPoolSize=50
c3p0.initialPoolSize=300

3、在spring中對DataAccess的配置如下:
Xml程式碼
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${c3p0.driverClass}"></property>
<property name="jdbcUrl" value="${c3p0.url}"></property>
<property name="user" value="${c3p0.user}"></property>
<property name="password" value="${c3p0.password}"></property>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>

<property name="acquireRetryDelay" value="1000"></property>
<property name="acquireRetryAttempts" value="60"></property>
<property name="breakAfterAcquireFailure" value="false"></property>
</bean>
<!--Hibernate SessionFatory-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="lobHandler" ref="lobHandler" />
<property name="configLocations">
<list>
<value>classpath*:/hibernate/hibernate.cfg.xml</value>
</list>
</property>
<property name="configurationClass"
value="org.hibernate.cfg.AnnotationConfiguration" />
<!-- 如果採用hbm 方式
<property name="mappingDirectoryLocations">
<list>
<value>
classpath*:/org/ustb/mis/model/hbm/
</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.cache.use_query_cache">
${hibernate.cache.use_query_cache}
</prop>
<prop key="hibernate.cache.provider_class">
${hibernate.cache.provider_class}
</prop>
lt;!-- 配置C3P0ConnectionProvider-->
<prop key="hibernate.connection.provider_class">
${hibernate.connection.provider_class}
</prop>
<prop key="hibernate.current_session_context_class">
${hibernate.current_session_context_class}
</prop>
</props>
</property>
</bean>

<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${c3p0.driverClass}"></property>
<property name="jdbcUrl" value="${c3p0.url}"></property>
<property name="user" value="${c3p0.user}"></property>
<property name="password" value="${c3p0.password}"></property>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>

<property name="acquireRetryDelay" value="1000"></property>
<property name="acquireRetryAttempts" value="60"></property>
<property name="breakAfterAcquireFailure" value="false"></property>
</bean>
<!--Hibernate SessionFatory-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="lobHandler" ref="lobHandler" />
<property name="configLocations">
<list>
<value>classpath*:/hibernate/hibernate.cfg.xml</value>
</list>
</property>
<property name="configurationClass"
value="org.hibernate.cfg.AnnotationConfiguration" />
<!-- 如果採用hbm 方式
<property name="mappingDirectoryLocations">
<list>
<value>
classpath*:/org/ustb/mis/model/hbm/
</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.cache.use_query_cache">
${hibernate.cache.use_query_cache}
</prop>
<prop key="hibernate.cache.provider_class">
${hibernate.cache.provider_class}
</prop>
<!-- 配置C3P0ConnectionProvider-->
<prop key="hibernate.connection.provider_class">
${hibernate.connection.provider_class}
</prop>
<prop key="hibernate.current_session_context_class">
${hibernate.current_session_context_class}
</prop>
</props>
</property>
</bean>

其中,屬性檔案通過如下方式讀入:
Xml程式碼
<!-- 屬性檔案讀入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:hibernate/jdbc.properties</value>
<value>classpath*:hibernate/hibernate.properties</value>
</list>
</property>
</bean>

<!-- 屬性檔案讀入 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:hibernate/jdbc.properties</value>
<value>classpath*:hibernate/hibernate.properties</value>
</list>
</property>
</bean>

對於各個c3p0屬性的意義及設定,很多文章都談到了,我參考的是: http://msq.iteye.com/的文章:C3P0連線池詳細配置。