type
Post
status
Published
date
Feb 1, 2021
slug
summary
DBCP、C3P0、Druid连接池的使用和常见配置
tags
项目方案
category
技术分享
icon
password

DBCP

DBCP 是Apache成员之一,在企业开发中也比较常见,也是tomcat内置的连接池

DBCP 工具类

public class DBCPUtils {    //1.定义常量 保存数据库连接的相关信息    public static final String DRIVERNAME = "com.mysql.jdbc.Driver";    public static final String URL = "jdbc:mysql://localhost:3306/db5?characterEncoding=UTF-8";    public static final String USERNAME = "root";    public static final String PASSWORD = "123456";    //2.创建连接池对象 (有DBCP提供的实现类)    public static BasicDataSource dataSource = new BasicDataSource();    //3.使用静态代码块进行配置    static{        dataSource.setDriverClassName(DRIVERNAME);        dataSource.setUrl(URL);        dataSource.setUsername(USERNAME);        dataSource.setPassword(PASSWORD);        dataSource.setMaxActive(20);   }    //4.获取连接的方法    public static Connection getConnection() throws SQLException {        //从连接池中获取连接        Connection connection = dataSource.getConnection();        return connection;   }    //5.释放资源方法    public static void close(Connection con, Statement statement){        if(con != null && statement != null){            try {                statement.close();                //归还连接                con.close();           } catch (SQLException e) {                e.printStackTrace();           }       }   }    public static void close(Connection con, Statement statement, ResultSet resultSet){        if(con != null && statement != null && resultSet != null){            try {                resultSet.close();                statement.close();                //归还连接                con.close();           } catch (SQLException e) {                e.printStackTrace();           }       }   } }

常见配置项

  • driverClassName 数据库驱动名称
  • url 数据库地址
  • username 用户名
  • password 密码
  • maxActive 最大连接数量
  • maxIdle 最大空闲连接
  • minIdle 最小空闲连接
  • initialSize 初始化连接

C3P0

XML 配置

c3p0-config.xml 配置文件名不可更改
放置在 src 或资源目录下,资源目录需要在 IDEA 中 mark as:
notion image
<c3p0-config> <!--默认配置-->  <default-config>      <property name="driverClass">com.mysql.jdbc.Driver</property>      <property name="jdbcUrl">jdbc:mysql://localhost:3306/db5?characterEncoding=UTF-8</property>      <property name="user">root</property>      <property name="password">123456</property>      <!-- initialPoolSize:初始化时获取三个连接,      取值应在minPoolSize与maxPoolSize之间。 -->      <property name="initialPoolSize">3</property>      <!-- maxIdleTime:最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。-->      <property name="maxIdleTime">60</property>      <!-- maxPoolSize:连接池中保留的最大连接数 -->      <property name="maxPoolSize">100</property>      <!-- minPoolSize: 连接池中保留的最小连接数 -->      <property name="minPoolSize">10</property>  </default-config>  <!--配置连接池mysql-->  <named-config name="mysql">      <property name="driverClass">com.mysql.jdbc.Driver</property>      <property name="jdbcUrl">jdbc:mysql://localhost:3306/db5</property>      <property name="user">root</property>      <property name="password">123456</property>      <property name="initialPoolSize">10</property>      <property name="maxIdleTime">30</property>      <property name="maxPoolSize">100</property>      <property name="minPoolSize">10</property>  </named-config> <!--配置连接池2,可以配置多个--> </c3p0-config>

常见配置

notion image

工具类

C3P0提供的核心工具类, ComboPooledDataSource ,
如果想使用连接池,就必须创建该类的对象
  • new ComboPooledDataSource(); 使用 默认配置
  • new ComboPooledDataSource("mysql"); 使用命名配置
public class C3P0Utils {    //1.创建连接池对象 C3P0对DataSource接口的实现类    //使用的配置是 配置文件中的默认配置    //public static ComboPooledDataSource dataSource = new ComboPooledDataSource();    //使用指定的配置    public static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");    //获取连接的方法    public static Connection getConnection() throws SQLException {        return dataSource.getConnection();   }    //释放资源    public static void close(Connection con, Statement statement){        if(con != null && statement != null){            try {                statement.close();                //归还连接                con.close();           } catch (SQLException e) {                e.printStackTrace();           }       }   }    public static void close(Connection con, Statement statement, ResultSet resultSet){        if(con != null && statement != null && resultSet != null){            try {                resultSet.close();                statement.close();                //归还连接                con.close();           } catch (SQLException e) {                e.printStackTrace();           }       }   } }

Druid

阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况

properties 配置文件

  • .properties 格式
  • 文件名任意,目录也任意
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/db5?characterEncoding=UTF-8 username=root password=123456 initialSize=5 maxActive=10 maxWait=3000

工具类

通过工厂来来获取 DruidDataSourceFactory类的createDataSource方法
createDataSource(Properties p) 方法参数可以是一个属性集对象
public class DruidUtils {    //1.定义成员变量    public static DataSource dataSource;    //2.静态代码块    static{        try {            //3.创建属性集对象            Properties p = new Properties();            //4.加载配置文件 Druid 连接池不能够主动加载配置文件 ,需要指定文件            InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");            //5. 使用Properties对象的 load方法 从字节流中读取配置信息            p.load(inputStream);            //6. 通过工厂类获取连接池对象            dataSource = DruidDataSourceFactory.createDataSource(p);       } catch (Exception e) {            e.printStackTrace();       }   }    //获取连接的方法    public static Connection getConnection(){        try {            return dataSource.getConnection();       } catch (SQLException e) {            e.printStackTrace();            return null;       }   }    //获取Druid连接池对象的方法    public static DataSource getDataSource(){        return dataSource;   }    //释放资源    public static void close(Connection con, Statement statement){        if(con != null && statement != null){            try {                statement.close();                //归还连接                con.close();           } catch (SQLException e) {                e.printStackTrace();           }       }   }    public static void close(Connection con, Statement statement, ResultSet resultSet){        if(con != null && statement != null && resultSet != null){            try {                resultSet.close();                statement.close();                //归还连接                con.close();           } catch (SQLException e) {                e.printStackTrace();           }       }   } }
自定义数据库连接池的实现Redis缓存设计案例