zoukankan      html  css  js  c++  java
  • 连接池

    连接池:程序不用自己来创建连接,用池来管理连接,连接池会默认创建n个初始连接,用的时候从里面捞几个上来,用完了再扔回去,可以重复利用,这个池子里的连接是用各种方法创建的,其中就包括用jdbc创建的

    dbcp连接池:

    导入jar包:commons-dbcp-1.4.jar

    public static void main(String[] args) throws Exception {
         //创建连接池对象
         BasicDataSource dataSource = new BasicDataSource();
         //配置连接参数(必写)
         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
         dataSource.setUrl("jdbc:mysql://localhost:3306/demo");
         dataSource.setUsername("root");
         dataSource.setPassword("root");
         
         //配置连接池参数(可选)
         dataSource.setInitialSize(10);//设置连接池的初始大小
         dataSource.setMaxActive(30);//设置连接池的最大连接数
         dataSource.setMaxIdle(5);//设置最大等待连接
         dataSource.setMaxWait(1000);//设置最大等待时间(毫秒),超出这个时间getConnection()方法会抛出异常
              
         //从连接池中得到连接
         Connection con = dataSource.getConnection();
    //测试连接 System.out.println(con); }

     ps:  

    org.apache.commons.dbcp.BasicDataSource implements javax.sql.DataSource 
    //DataSource接口是sun为各大数据库厂商提供的连接池接口,BasicDataSource是mysql公司对这个接口的实现

    c3p0连接池:

    导入jar包:c3p0-0.9.2-pre1.jar, mchange-commons-0.2.jar,c3p0-oracle-thin-extras-0.9.2-pre1.jar(oracle专用jar包,不用oracle可以不导)

    public static void main(String[] args) throws Exception {
            //创建连接池对象
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            //配置连接参数(必须)
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo");
            dataSource.setUser("root");
            dataSource.setPassword("root");
            
            //配置连接池参数(可选)
            dataSource.setAcquireIncrement(5);//设置连接增量
            dataSource.setInitialPoolSize(20);//设置连接池初始连接数量
            dataSource.setMaxPoolSize(100);//设置最大连接数量
            dataSource.setMinPoolSize(3);//设置最小连接数量,低于这个数量,会按增量值增加连接
            
            
            Connection con = dataSource.getConnection();
            System.out.println(con);
            //把连接归还给连接池
            con.close();
            
        }

    c3p0也可以使用配置文件,文件名必须为c3p0-config.xml,并且放在类路径下

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <default-config>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/demo</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> <named-config name="oracle-config">
    <property name="jdbcUrl">jdbc:mysql://localhost:1521/demo</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">root</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>

    使用默认配置

    public static void main(String args[]) throws Exception {
         ComboPooledDataSource dataSource = new ComboPooledDataSource();//无参,默认使用xml中default-config标签中的配置
         Connection con = dataSource.getConnection();
         System.out.print(con);
         con.close();   
    }

    使用命名配置

    public static void main(String args[]) throws Exception {
         ComboPooledDataSource dataSource = new ComboPooledDataSource("oralce-config");//带参,使用xml中named-config标签的配置 
    Connection con = dataSource.getConnection();
    System.out.print(con);
    con.close();
    }
  • 相关阅读:
    CF140CNew Year Snowmen
    ZR10.1青岛集训三地联考
    CF1228——记一次和紫名失之交臂的CF
    CF1220
    codeforce 382 div2 E —— 树状dp
    codeforce 381 div2
    codeforce 380(div.2)
    Bishops Alliance—— 最大上升子序列
    codeforce 379(div.2)
    模板——BigInteger
  • 原文地址:https://www.cnblogs.com/sflik/p/4602415.html
Copyright © 2011-2022 走看看