1.首先配置数据源 (依旧使用的是 org.springframework.jdbc.datasource.DriverManagerDataSource)
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>${dbconn.docstore.url}</value>
</property>
<property name="username">
<value>${dbconn.docstore.userName}</value>
</property>
<property name="password">
<value>${dbconn.docstore.password}</value>
</property>
</bean>
================================
2.关于连接池的配置放在了配置sessionfactory的地方:
<bean id="ppSessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="jtaTransactionManager"
ref="atomikosTransactionManager" />
<property name="mappingDirectoryLocations">
<list>
<value>${dbconn.docstore.mappingDirectory}</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${dbconn.docstore.dialect}
</prop>
<prop key="hibernate.show_sql">
${dbconn.docstore.showsql}
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">
false
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<!--
for jta compatilibility (jta)
org.hibernate.context.JTASessionContext
-->
<prop key="current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</prop>
<prop key="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</prop>
<prop key="hibernate.c3p0.max_size">4</prop>
<prop key="hibernate.c3p0.min_size">2</prop>
<prop key="hibernate.c3p0.timeout">15000</prop>
<prop key="hibernate.c3p0.max_statements">100</prop>
<prop key="hibernate.c3p0.idle_test_period">120</prop>
<prop key="hibernate.c3p0.acquire_increment">2</prop>
</props>
</property>
</bean>
=====================
以上代码是spring和hibernate和C3P0同时使用,其中红色标注的是重点代码,关于配置C3P0连接池的。【其实就是往常的数据源照用,在sessionfactory中配置C3P0参数就行】
<!-- 最大连接数 -->
<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>
参见:http://www.blogjava.net/feicer/articles/C3P0_config.html
===============================================================
如果不使用hibernate的话,配置可能是,将连接池的配置放在了数据源的配置里:
<bean id="phDS" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${dbconn.docstore.driverName}" />
<property name="jdbcUrl" value="${dbconn.docstore.url}" />
<property name="user" value="${dbconn.docstore.userName}" />
<property name="password" value="${dbconn.docstore.password}" />
<property name="minPoolSize" value="${jdbc.miniPoolSize}" />
<property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
<property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
<property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
<property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
<property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
<property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}" />
<property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}" />
<property name="automaticTestTable" value="${jdbc.automaticTestTable}" />
<property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}" />
<property name="checkoutTimeout" value="${jdbc.checkoutTimeout}" />
</bean>