C3P0是一个开放源代码的JDBC连接池,它在lib目录中与Hibernate一起发布,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSources 对象。
c3p0.acquireIncrement=3 c3p0.initialPoolSize=3 c3p0.idleConnectionTestPeriod=900 c3p0.minPoolSize=2 c3p0.maxPoolSize=50 c3p0.maxStatements=100 c3p0.numHelperThreads=10 c3p0.maxIdleTime=600
<property name="acquireIncrement"> <value>${c3p0.acquireIncrement}</value> </property> <property name="initialPoolSize"> <value>${c3p0.initialPoolSize}</value> </property> <property name="minPoolSize"> <value>${c3p0.minPoolSize}</value> </property> <property name="maxPoolSize"> <value>${c3p0.maxPoolSize}</value> </property> <property name="maxIdleTime"> <value>${c3p0.maxIdleTime}</value> </property> <property name="idleConnectionTestPeriod"> <value>${c3p0.idleConnectionTestPeriod}</value> </property> <property name="maxStatements"> <value>${c3p0.maxStatements}</value> </property> <property name="numHelperThreads"> <value>${c3p0.numHelperThreads}</value> </property>
<property name="acquireIncrement">3</property>
<property name="acquireRetryAttempts">30</property>
<property name="acquireRetryDelay">1000</property>
<property name="autoCommitOnClose">false</property>
<property name="automaticTestTable">Test</property>
<property name="breakAfterAcquireFailure">false</property>
<!--当连接池用完时客户端调用getConnection()后等待获取新连接的时间,
超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。efault: 0 -->
<property name="checkoutTimeout">100</property>
Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester-->
<property name="connectionTesterClassName"></property>
<property name="factoryClassLocation">null</property>
<property name="forceIgnoreUnresolvedTransactions">false</property>
<property name="idleConnectionTestPeriod">60</property>
<property name="initialPoolSize">3</property>
<property name="maxIdleTime">60</property>
<property name="maxPoolSize">15</property>
<property name="maxStatements">100</property>
<property name="maxStatementsPerConnection"></property>
<property name="numHelperThreads">3</property>
<property name="overrideDefaultUser">root</property>
<property name="overrideDefaultPassword">password</property>
<property name="password"></property>
<property name="preferredTestQuery">select id from test where id=1</property>
<property name="propertyCycle">300</property>
<property name="testConnectionOnCheckout">false</property>
<property name="testConnectionOnCheckin">true</property>
<!--用户名。Default: null-->
<property name="user">root</property>
在Hibernate(spring管理)中的配置: <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass"><value>oracle.jdbc.driver.OracleDriver</value></property> <property name="jdbcUrl"><value>jdbc:oracle:thin:@localhost:1521:Test</value></property> <property name="user"><value>Kay</value></property> <property name="password"><value>root</value></property> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="10" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="100" /> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="1800" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3" /> <property name="maxStatements" value="1000" /> <property name="initialPoolSize" value="10" /> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60" /> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="30" /> <property name="breakAfterAcquireFailure" value="true" /> <property name="testConnectionOnCheckout" value="false" /> </bean>
########################### ### C3P0 Connection Pool### ########################### #hibernate.c3p0.max_size 2 #hibernate.c3p0.min_size 2 #hibernate.c3p0.timeout 5000 #hibernate.c3p0.max_statements 100 #hibernate.c3p0.idle_test_period 3000 #hibernate.c3p0.acquire_increment 2 #hibernate.c3p0.validate false 在hibernate.cfg.xml文件里面加入如下的配置: <!-- 最大连接数 --> <property name="hibernate.c3p0.max_size">20</property> <!-- 最小连接数 --> <property name="hibernate.c3p0.min_size">5</property> <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 --> <property name="hibernate.c3p0.timeout">120</property> <!-- 最大的PreparedStatement的数量 --> <property name="hibernate.c3p0.max_statements">100</property> <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒--> <property name="hibernate.c3p0.idle_test_period">120</property> <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 --> <property name="hibernate.c3p0.acquire_increment">2</property> <!-- 每次都验证连接是否可用 --> <property name="hibernate.c3p0.validate">true</property>