c3p0在spring下的配置过程

来源:互联网 发布:android性能优化面试 编辑:程序博客网 时间:2024/06/05 11:56
前几天在配置hibernate的c3p0连接池时报出了死锁的异常。经过一顿的学习,现在项目中已经解决了这个问题,现将其配置记录于下,供自己和有需要的人查阅。
1、在hibernate.properties中添加:
Xml代码  收藏代码
  1. hibernate.connection.provider_class =org.hibernate.connection.C3P0ConnectionProvider  


2、将c3p0的常规配置我新建在了自己写的properites中,取名:jdbc.properties,关于c3p0的代码如下:
Xml代码  收藏代码
  1. ######C3P0 MySQL config #######  
  2. c3p0.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8   
  3. c3p0.user=root  
  4. c3p0.password=root  
  5.   
  6. c3p0.driverClass=com.mysql.jdbc.Driver  
  7. c3p0.acquireIncrement=1  
  8. c3p0.maxIdleTime=60  
  9. c3p0.maxPoolSize=200  
  10. c3p0.minPoolSize=50  
  11. c3p0.initialPoolSize=300  


3、在spring中对DataAccess的配置如下:
Xml代码  收藏代码
  1. <bean id="dataSource"  
  2.     class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  3.     destroy-method="close">  
  4.     <property name="driverClass" value="${c3p0.driverClass}"></property>  
  5.     <property name="jdbcUrl" value="${c3p0.url}"></property>  
  6.     <property name="user" value="${c3p0.user}"></property>  
  7.     <property name="password" value="${c3p0.password}"></property>  
  8.     <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>  
  9.     <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>  
  10.     <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>  
  11.     <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>  
  12.     <property name="minPoolSize" value="${c3p0.minPoolSize}"></property>  
  13.       
  14.     <property name="acquireRetryDelay" value="1000"></property>  
  15.     <property name="acquireRetryAttempts" value="60"></property>  
  16.     <property name="breakAfterAcquireFailure" value="false"></property>  
  17. </bean>  
  18. <!--Hibernate SessionFatory-->  
  19. <bean id="sessionFactory"  
  20.     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  21.     <property name="dataSource" ref="dataSource" />  
  22.     <property name="lobHandler" ref="lobHandler" />  
  23.     <property name="configLocations">  
  24.         <list>  
  25.             <value>classpath*:/hibernate/hibernate.cfg.xml</value>  
  26.         </list>  
  27.     </property>  
  28.     <property name="configurationClass"  
  29.         value="org.hibernate.cfg.AnnotationConfiguration" />  
  30.     <!-- 如果采用hbm 方式  
  31.         <property name="mappingDirectoryLocations">  
  32.         <list>  
  33.         <value>  
  34.         classpath*:/org/ustb/mis/model/hbm/  
  35.         </value>  
  36.         </list>  
  37.         </property>  
  38.     -->  
  39.     <property name="hibernateProperties">  
  40.         <props>  
  41.             <prop key="hibernate.dialect">  
  42.                 ${hibernate.dialect}  
  43.             </prop>  
  44.             <prop key="hibernate.show_sql">  
  45.                 ${hibernate.show_sql}  
  46.             </prop>  
  47.             <prop key="hibernate.cache.use_query_cache">  
  48.                 ${hibernate.cache.use_query_cache}  
  49.             </prop>  
  50.             <prop key="hibernate.cache.provider_class">  
  51.                 ${hibernate.cache.provider_class}  
  52.             </prop>  
  53. lt;!-- 配置C3P0ConnectionProvider-->  
  54.             <prop key="hibernate.connection.provider_class">  
  55.                 ${hibernate.connection.provider_class}  
  56.             </prop>  
  57.             <prop key="hibernate.current_session_context_class">  
  58.                 ${hibernate.current_session_context_class}  
  59.             </prop>  
  60.         </props>  
  61.     </property>  
  62. </bean>  

其中,属性文件通过如下方式读入:
Xml代码  收藏代码
  1. <!-- 属性文件读入 -->  
  2. <bean id="propertyConfigurer"  
  3.     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  4.     <property name="locations">  
  5.         <list>  
  6.             <value>classpath*:hibernate/jdbc.properties</value>  
  7.             <value>classpath*:hibernate/hibernate.properties</value>  
  8.         </list>  
  9.     </property>  
  10. </bean>  

对于各个c3p0属性的意义及设置,很多文章都谈到了,我参考的是: http://msq.iteye.com/的文章:C3P0连接池详细配置。