利用c3p0配置数据库连接

本文介绍如何使用 C3P0 连接池配置 MySQL 和 Oracle 数据库,并结合 Hibernate 实现数据库操作。包括配置文件示例、连接池参数设置及 Hibernate 相关属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用c3p0配置数据库连接

一.连接mysql数据库

<beanid="dataSource"
class
="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method
="close"abstract="false"singleton="true"
lazy-init
="default"autowire="default"dependency-check="default">
<propertyname="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<propertyname="jdbcUrl">
<value>jdbc:mysql://192.168.0.1/databasename?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true</value>
</property>
<propertyname="user">
<value>username</value>
</property>
<propertyname="password">
<value>password</value>
</property>
<propertyname="minPoolSize">
<value>10</value>
</property>
<propertyname="maxPoolSize">
<value>20</value>
</property>
<propertyname="initialPoolSize">
<value>10</value>
</property>
</bean>
<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="dataSource"></ref>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<propkey="hibernate.show_sql">
false
</prop>
<propkey="hibernate.generate_statistics">true</prop>
<propkey="hibernate.cache.use_query_cache">
true
</prop>
<propkey="hibernate.cache.use_second_level_cache">
true
</prop>
<propkey="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider
</prop>
<propkey="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</prop>
<propkey="hibernate.query.substitutions">
true1,false0,yes'Y',no'N'
</prop>
<prop
key="hibernate.bytecode.use_reflection_optimizer">
true
</prop>
<propkey="hibernate.use_outer_join">true</prop>
<propkey="hibernate.prepare_sql">true</prop>
<propkey="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
</props>
</property>

<propertyname="mappingResources">
<list>
<value>xxx/xx/xx.hbm.xml</value>
<value>xxx/xx/xx.hbm.xml</value>
</list>
</property>
</bean>

二.连接Oracle数据库
<beanid="dataSource"
class
="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method
="close"abstract="false"singleton="true"
lazy-init
="default"autowire="default"dependency-check="default">
<propertyname="driverClass">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<propertyname="jdbcUrl">
<value>jdbc:oracle:thin:@192.168.0.1:1521:orcl</value>
</property>
<propertyname="user">
<value>username</value>
</property>
<propertyname="password">
<value>password</value>
</property>
<propertyname="minPoolSize">
<value>10</value>
</property>
<propertyname="maxPoolSize">
<value>20</value>
</property>
<propertyname="initialPoolSize">
<value>10</value>
</property>
</bean>

<beanid="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="dataSource"></ref>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">
org.hibernate.dialect.OracleDialect
</prop>
<propkey="hibernate.show_sql">
false
</prop>
<propkey="hibernate.generate_statistics">true</prop>
<propkey="hibernate.cache.use_query_cache">
true
</prop>
<propkey="hibernate.cache.use_second_level_cache">
true
</prop>
<propkey="hibernate.cache.provider_class">
net.sf.ehcache.hibernate.EhCacheProvider
</prop>
<propkey="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</prop>
<propkey="hibernate.query.substitutions">
true1,false0,yes'Y',no'N'
</prop>
<prop
key="hibernate.bytecode.use_reflection_optimizer">
true
</prop>
<propkey="hibernate.use_outer_join">true</prop>
<propkey="hibernate.prepare_sql">true</prop>
<propkey="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory
</prop>
</props>
</property>

<propertyname="mappingResources">
<list>
<value>xxx/xx/xx.hbm.xml</value>
<value>xxx/xx/xx.hbm.xml</value>
</list>
</property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值