spring+hibernate的c3p0 连接池配置
时间:2010-12-23 来源:xue2
网上关于C3P0在spring中的配置,几乎没有完全正确的(至少我还没发现)。查了c3p0的文档,又试验过N次。得出如下配置是正确的:
<!-- com.mchange.v2.c3p0.ComboPooledDataSource -->
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="jdbcUrl"
value="jdbc:oracle:thin:@127.0.0.1:1521:db">
</property>
<property name="user" value="123"></property>
<property name="password" value="123"></property>
<property name="minPoolSize"><value>1</value></property>
<property name="maxPoolSize"><value>20</value></property>
<property name="maxIdleTime"><value>1800</value></property>
<property name="acquireIncrement"><value>2</value></property>
<property name="maxStatements"><value>0</value></property>
<property name="initialPoolSize"><value>2</value></property>
<property name="idleConnectionTestPeriod"><value>1800</value></property>
<property name="acquireRetryAttempts"><value>30</value></property>
<property name="breakAfterAcquireFailure"><value>true</value></property>
<property name="testConnectionOnCheckout"><value>false</value></property>
</bean>
<!--
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@221.232.148.68:1521:starmap">
</property>
<property name="username" value="htgis"></property>
<property name="password" value="htgis"></property>
</bean>
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<!--
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.show_sql">false</prop>-->
</props>
</property>
<property name="mappingResources">
<list>
<value>ht/gisoa/entity/CmppSubmitCache.hbm.xml</value>
<value>ht/gisoa/entity/CmppDeliver.hbm.xml</value>
<value>ht/gisoa/entity/CmppSubmit.hbm.xml</value>
<value>ht/gisoa/entity/CmppReport.hbm.xml</value>
<value>ht/gisoa/entity/SGpstraceinfo.hbm.xml</value>
<value>ht/gisoa/entity/SDeviceinfo.hbm.xml</value>
<value>ht/gisoa/entity/SLbstraceinfo.hbm.xml</value>
<value>ht/gisoa/entity/Lbsschemainfo.hbm.xml</value>
</list>
</property>
</bean>
2.导入包
3.配置c3p0a 1. 下载c3p0
http://sourceforge.net/projects/c3p0
2.在工程中导入c3p0包
3.配置c3p0 数据库连接池(我用的Oracle)
3.1 我用的是Spring+Hibernate,数据库是在Spring中配置
直接把<datasource>改为c3p0的配置就可以了
如下:
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.1.3:1521:HZORA6"></property>
<property name="user" value="hz1001"></property>
<property name="password" value="123"></property> <property name="maxPoolSize" value="40"></property>
<property name="minPoolSize" value="1"></property>
<property name="initialPoolSize" value="1"></property>
<property name="maxIdleTime" value="20"></property>
</bean> 以下的<SessionFactory>配置,只需要引用<DataSource>bean就可以了 <bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">