数据库连接池(c3p0和德鲁伊)

目录

连接池介绍

c3p0连接池

传统方法引入jar包

配置文件

德鲁伊连接池

德鲁伊工具类


传统jdbc数据库使用DriverManger来获取,每次向数据库建立连接需要将Connection加载到内存中,频繁的操作会造成占用很多系统资源,造成服务器崩溃,每次连接完如果程序出现异常会造成数据泄露,所以我们采用数据库连接池技术

连接池介绍

1.预先在缓存池中放入一定数量的连接,当需要建立数据库时,只需将缓存池中取出一个,使用完毕后再放回

2.数据据库连接池负责分配,管理和释放数据库连接,他允许应用程序重复使用一个现有的数据库连接,而不是建立一个

3.当应用程序向连接池请求的连接超过最大连接数量时,这些请求将被加入到等待队列中

c3p0连接池

速度较慢,但稳定性好

传统方法引入jar包

java 复制代码
public void testC3P0_01() throws Exception {
 
        //1. 创建一个数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        //2. 通过配置文件mysql.properties 获取相关连接的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //读取相关的属性值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
 
        //给数据源 comboPooledDataSource 设置相关的参数
        //注意:连接管理是由 comboPooledDataSource 来管理
        comboPooledDataSource.setDriverClass(driver);
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);
 
        //设置初始化连接数
        comboPooledDataSource.setInitialPoolSize(10);
        //最大连接数
        comboPooledDataSource.setMaxPoolSize(50);    
        Connection connection = comboPooledDataSource.getConnection(); //这个方法就是从 DataSource 接口实现的
        connection.close(); 
}

配置文件

java 复制代码
 public void testC3P0_02() throws SQLException {
 
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("db");
        Connection connection = comboPooledDataSource.getConnection();
        connection.close();
}

德鲁伊连接池

是阿里提供的数据库连接池,集dbcp,c3p0,proxool优点于一身

java 复制代码
public void testDruid() throws Exception {
        //1. 加入 Druid jar包
        //2. 加入 配置文件 druid.properties , 将该文件拷贝项目的src目录
        //3. 创建Properties对象, 读取配置文件
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\druid.properties"));
 
        //4. 创建一个指定参数的数据库连接池, Druid连接池
        DataSource dataSource =
                DruidDataSourceFactory.createDataSource(properties);
            Connection connection = dataSource.getConnection();
            connection.close();
}

德鲁伊工具类

将数据库进行封装处理

java 复制代码
public class JDBCUtilsByDruid {
 
    private static DataSource ds;
 
    //在静态代码块完成 ds初始化
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    //编写getConnection方法
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
 
    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
 
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
相关推荐
阿里嘎多哈基米1 天前
SQL 层面行转列
数据库·sql·状态模式·mapper·行转列
抠脚学代码1 天前
Ubuntu Qt x64平台搭建 arm64 编译套件
数据库·qt·ubuntu
jakeswang1 天前
全解MySQL之死锁问题分析、事务隔离与锁机制的底层原理剖析
数据库·mysql
Heliotrope_Sun1 天前
Redis
数据库·redis·缓存
一成码农1 天前
MySQL问题7
数据库·mysql
吃饭最爱1 天前
JUnit技术的核心和用法
数据库·oracle·sqlserver
专注API从业者1 天前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python
雨落Liy1 天前
SQL 函数从入门到精通:原理、类型、窗口函数与实战指南
数据库·sql
Kt&Rs1 天前
MySQL复制技术的发展历程
数据库·mysql
小小菜鸡ing1 天前
pymysql
java·服务器·数据库