利用c3p0配置数据库连接

本文介绍了如何使用Spring框架配置MySQL和Oracle数据库连接,并详细展示了数据源和会话工厂的XML配置示例。

一.连接mysql数据库

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

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

二.连接Oracle数据库

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

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

        
<property name="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、付费专栏及课程。

余额充值