zoukankan      html  css  js  c++  java
  • C3P0数据库连接池的java实现

    1、配置准备

    • 导入jar包
      • c3p0-0.9.2-pre1.jar
      • mchange-commons-0.2.jar
      • 数据库驱动包,如:mysql-connector-java-5.1.28-bin.jar
    • 配置文件名称必须叫c3p0-config.xml
    • 配置文件位置必须在src下

    2、配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <!-- 这是默认配置信息 -->
        <default-config> 
            <!-- 连接四大参数配置 -->
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb3</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="user">root</property>
            <property name="password">123</property>
            <!-- 池参数配置 -->
            <!-- 连接池初始化时建立的连接数 默认值是3 -->  
            <property name="initialPoolSize">3</property>  
            <!-- 连接的最大空闲时间  单位秒 默认是0-代表永远不会断开连接  超过设定时间的空闲连接将会断开 -->  
            <property name="maxIdleTime">30</property>  
            <!-- 连接池中拥有的最大连接数 默认值为15个 -->  
            <property name="maxPoolSize">20</property>  
            <!-- 连接池中保持的最小连接数  默认值为3个-->  
            <property name="minPoolSize">3</property>  
            <!-- 将连接池的连接数保持在minpoolsize 必须小于maxIdleTime设置  默认值为0代表不处理  单位秒 -->  
            <property name="maxIdleTimeExcessConnections">15</property>  
            
        </default-config>
        
        <!-- 专门为oracle提供的配置信息 -->
        <named-config name="oracle-config"> 
            <property name="jdbcUrl">jdbc:oracle:oci8:@dataname</property>
            <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
            <property name="user">root</property>
            <property name="password">123</property>
            <property name="acquireIncrement">3</property>
            <property name="initialPoolSize">10</property>
            <property name="minPoolSize">2</property>
            <property name="maxPoolSize">10</property>
        </named-config>
    
    </c3p0-config>

    3、实现案例

    import java.beans.PropertyVetoException;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import org.junit.Test;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    /**
     * c3p0*/
    public class Demo1 {
        /**
         * 代码配置,该配置不需要配置文件
         * @throws PropertyVetoException
         * @throws SQLException
         */
        @Test
        public void fun1() throws PropertyVetoException, SQLException {
            // 创建连接池对象
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            
            // 对池进行四大参数的配置
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb");
            dataSource.setUser("root");
            dataSource.setPassword("123");
            
            // 池配置
            dataSource.setAcquireIncrement(5);
            dataSource.setInitialPoolSize(20);
            dataSource.setMinPoolSize(2);
            dataSource.setMaxPoolSize(50);
            
            Connection con = dataSource.getConnection();
            System.out.println(con);
            con.close(); //该close()方法不是关闭连接,而是将连接归还给连接池。
        }
        
        /**
         * 配置文件的默认配置,若代码中同时存在配置,代码中的配置参数优先
         * @throws SQLException 
         */
        @Test
        public void fun2() throws SQLException{
            /**
             * 在创建连接池对象时,这个对象就会自动加载配置文件!不用我们来指定
             */
            ComboPooledDataSource dataSource  = new ComboPooledDataSource();
            
            Connection con = dataSource.getConnection();
            System.out.println(con);
            con.close();
        }
        
        /**
         * 使用命名配置信息
         * @throws SQLException
         */
        @Test
        public void fun3() throws SQLException{
            /**
             * 构造器的参数指定命名配置元素的名称!
             * <named-config name="oracle-config"> 
             */
            ComboPooledDataSource dataSource  = new ComboPooledDataSource("oracle-config");
            
            Connection con = dataSource.getConnection();
            System.out.println(con);
            con.close();
        }
    }
  • 相关阅读:
    Umbraco建站指南[0]:前言
    项目开发中一些不得其解的问题
    Maven Install 的傻问题
    html5 audio/video 操作
    CentOS7.3安装MySQL5.7
    Maven将独立jar包安装到本地库
    MyBatis 中 foreach 语句处理 List<Integer>类型
    站内信系统的设计思路
    Spring+MyBatis项目开发代码步骤
    webpack 配置eslint-standard
  • 原文地址:https://www.cnblogs.com/hehaiyang/p/4704608.html
Copyright © 2011-2022 走看看