---
title: "Spring配置连接池"
author: "conect"
date: "2008-05-30T20:28:26Z"
tags: []
slug: "spring"
draft: false
lastmod: "2008-05-30T20:28:26Z"
id: 198826
url: "https://conect.iteye.com/blog/198826"
---

Spring 配置proxool连接池

在我的工程中，文件名叫hibernate.cfg.xml，和entity放在一起

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)"  
 xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx)"  
 xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd](http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)  
    [http://www.springframework.org/schema/context](http://www.springframework.org/schema/context) [http://www.springframework.org/schema/context/spring-context-2.5.xsd](http://www.springframework.org/schema/context/spring-context-2.5.xsd)  
    [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.5.xsd](http://www.springframework.org/schema/tx/spring-tx-2.5.xsd)"&gt;

 &lt;bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"&gt;  
  &lt;property name="driver" value="${hibernate.driverClass}" /&gt;

或者直接写成&lt;property name="driver"&gt;               &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;&lt;/property&gt;  
  &lt;property name="driverUrl" value="${hibernate.jbdc.url}" /&gt;  
  &lt;property name="user" value="${hibernate.jbdc.user}" /&gt;  
  &lt;property name="password" value="${hibernate.jdbc.password}" /&gt;

&lt;!-- &lt;alias&gt;是连接池的别名，proxoolPool连接池的一种，此外经常使用的连接池还有C3P0 --&gt;  
  &lt;property name="alias" value="proxoolPool" /&gt;

&lt;!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回

           收,超时的销毁 （默认30秒）--&gt;  
  &lt;property name="houseKeepingSleepTime" value="30000" /&gt;

&lt;!-- 最少保持的空闲连接数 （默认5个）--&gt;  
  &lt;property name="prototypeCount" value="20" /&gt;

&lt;!-- 设置连接池内生成的最大连接数/在连接池中所有数据库连接的最大数目（默认15个） --&gt;  
  &lt;property name="maximumConnectionCount" value="${hibernate.jdbc.maxConn}" /&gt;

&lt;!-- 设置连接池内生成的最小连接数/在连接池中可用的数据库连接的最少数目（默认5个）--&gt;  
  &lt;property name="minimumConnectionCount" value="${hibernate.jdbc.minConn}" /&gt;

&lt;!-- 在Consonl中显示sql --&gt;  
  &lt;property name="trace" value="true" /&gt;  
  &lt;property name="verbose" value="true" /&gt;  
 &lt;/bean&gt;

 &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;  
  &lt;property name="dataSource" ref="dataSource" /&gt;  
  &lt;property name="schemaUpdate" value="${hibernate.schemaUpdate}" /&gt;  
  &lt;property name="hibernateProperties"&gt;  
   &lt;props&gt;  
    &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;  
    &lt;prop key="hibernate.show\_sql"&gt;${hibernate.show\_sql}&lt;/prop&gt;  
    &lt;prop key="hibernate.format\_sql"&gt;true&lt;/prop&gt;  
    &lt;prop key="hibernate.use\_outer\_join"&gt;true&lt;/prop&gt;  
    &lt;prop key="hibernate.jdbc.fetch\_size"&gt;50&lt;/prop&gt;  
    &lt;prop key="hibernate.jdbc.use\_streams\_for\_binary"&gt;true&lt;/prop&gt;  
    &lt;prop key="hibernate.bytecode.use\_reflection\_optimizer"&gt;true&lt;/prop&gt;  
    &lt;prop key="hibernate.query.substitutions"&gt;true 1,false 0,yes 'Y',no 'N'&lt;/prop&gt;  
   &lt;/props&gt;  
  &lt;/property&gt;  
  &lt;property name="annotatedClasses"&gt;  
   &lt;list&gt;  
    &lt;value&gt;com.mysteel.entity.xxx&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
  &lt;property name="sessionFactory" ref="sessionFactory" /&gt;  
 &lt;/bean&gt;

&lt;/beans&gt;

第二个bean即从  
&lt;bean：id="sessionFactory"...&gt;到&lt;/bean&gt;是因为使用 Spring 框架，可以使用 AnnotationSessionFactoryBean  
类轻松建立一个基于注释的 Hibernate 会话工厂

&lt;!-- SessionFactory:使用AnnotationSessionFactoryBean可以直接在Spring中配置一个SessionFactory,而不必使用Hibernate特有的hibernate.cfg.xml配置文件。 --&gt;

Spring配C3P0连接池

applicationContext.xml 文件：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:jee="http://www.springframework.org/schema/jee"

    xsi:schemaLocation="http://www.springframework.org/schema/beans [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)

            [http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee) [http://www.springframework.org/schema/jee/spring-jee-2.0.xsd](http://www.springframework.org/schema/jee/spring-jee-2.0.xsd)"&gt;

    &lt;bean id="dataSource"

        class="com.mchange.v2.c3p0.ComboPooledDataSource"

        destroy-method="close"&gt;

        &lt;property name="driverclass"&gt;

            &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;

        &lt;/property&gt;

        &lt;property name="jdbcUrl"&gt;

            &lt;value&gt;jdbc:mysql://192.168.3.110:3306/DBName?useUnicode=true&amp;characterEncoding=GBK&lt;/value&gt;

        &lt;/property&gt;

        &lt;property name="user"&gt;

            &lt;value&gt;root&lt;/value&gt;

        &lt;/property&gt;

        &lt;property name="password"&gt;

            &lt;value&gt;root&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--连接池中保留的最小连接数。--&gt;

        &lt;property name="minPoolSize"&gt;

            &lt;value&gt;5&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--连接池中保留的最大连接数。Default: 15 --&gt;

        &lt;property name="maxPoolSize"&gt;

            &lt;value&gt;30&lt;/value&gt;

        &lt;/property&gt;

 

&lt;!--初始化时获取的连接数，取值应在minPoolSize与maxPoolSize之间。Default: 3 --&gt;

        &lt;property name="initialPoolSize"&gt;

            &lt;value&gt;10&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --&gt;

        &lt;property name="maxIdleTime"&gt;

            &lt;value&gt;60&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --&gt;

        &lt;property name="acquireIncrement"&gt;

            &lt;value&gt;5&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--JDBC的标准参数，用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements  
  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。  
  如果maxStatements与maxStatementsPerConnection均为0，则缓存被关闭。Default: 0--&gt;

        &lt;property name="maxStatements"&gt;

            &lt;value&gt;0&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--每60秒检查所有连接池中的空闲连接。Default: 0 --&gt;

        &lt;property name="idleConnectionTestPeriod"&gt;

            &lt;value&gt;60&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --&gt;

        &lt;property name="acquireRetryAttempts"&gt;

            &lt;value&gt;30&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效  
  保留，并在下次调用getConnection()的时候继续尝试获取连接。如果设为true，那么在尝试  
  获取连接失败后该数据源将申明已断开并永久关闭。Default: false--&gt;

        &lt;property name="breakAfterAcquireFailure"&gt;

            &lt;value&gt;true&lt;/value&gt;

        &lt;/property&gt;

 

 &lt;!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的  
  时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable  
  等方法来提升连接测试的性能。Default: false --&gt;

        &lt;property name="testConnectionOnCheckout"&gt;

            &lt;value&gt;false&lt;/value&gt;

        &lt;/property&gt;

    &lt;/bean&gt;

    &lt;!-- Hibernate SessionFactory --&gt;

    &lt;bean id="sessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;

        &lt;property name="dataSource"&gt;

            &lt;ref local="dataSource" /&gt;

        &lt;/property&gt;

        &lt;property name="mappingResources"&gt;

            &lt;list&gt;

                &lt;value&gt;com/xh/hibernate/vo/User.hbm.xml&lt;/value&gt;

            &lt;/list&gt;

        &lt;/property&gt;

        &lt;property name="hibernateProperties"&gt;

            &lt;props&gt;

                &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;

                &lt;prop key="hibernate.show\_sql"&gt;true&lt;/prop&gt;

                &lt;prop key="hibernate.generate\_statistics"&gt;true&lt;/prop&gt;

                &lt;prop key="hibernate.connection.release\_mode"&gt;auto&lt;/prop&gt;

                &lt;prop key="hibernate.autoReconnect"&gt;true&lt;/prop&gt;

            &lt;/props&gt;

        &lt;/property&gt;

    &lt;/bean&gt;

&lt;/beans&gt;