数据库连接池

提出:数据库开发中存在问题,每次客户请求,在服务器端都单独创建一个连接操作数据库,当并发访问量非常大,很容易造成内存溢出,而且创建连接、释放连接资源非常消耗服务器性能。

原理: 在服务器端一次性创建多个连接,将多个连接保存在一个连接池对象中,当请求需要操作数据库时,不会为请求创建新的连接,而是直接从连接池中获得一个连接,操作数据库结束,并不需要真正关闭连接,而是将连接放回到连接池中。节省创建连接、释放连接 资源。

在实际开发中,不会自己实现连接池,使用开源免费连接池。
Apache commons-dbcp 连接池、c3p0 数据库连接池、Tomcat内部提供数据库连接池
1. 当使用Apache DBCP 需要下载 commons-dbcp.jar commons-pool.jar
* apache commons 子项目 zip包没有快速入门文档 只有API
手动设置四个参数
编写properties配置文件 —— Properties对象加载文件
配置文件:

driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql:///day
username = root
password =12345678
public void demo2() throws Exception {
        // 读取dbcp.properties ---- Properties
        Properties properties = new Properties();
        properties.load(new FileInputStream(this.getClass().getResource(
                "/dbcp.properties").getFile()));

        DataSource bgs= BasicDataSourceFactory
                .createDataSource(properties);

// 连接池需要创建连接--需要四个参数
    //  bds.setDriverClassName("com.mysql.jdbc.Driver");
    //  bds.setUrl("jdbc:mysql:///day");
    //  bds.setUsername("root");
    //  bds.setPassword("12345678");
        // 从连接池中获取连接
        Connection conn = bgs.getConnection();
        String sql = "select * from account";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }

        JDBCUtils.release(rs, stmt, conn);

    }

2.下载c3p0 解压 doc目录 存在c3p0 使用入门,ComboPooledDataSource 手动设置参数,配置文件 在src目录新建 c3p0-config.xml

自定义配置可以有很多个
在实际软件系统中,测试环境、开发环境、线上数据库 是不同数据库

Basic Pool Configuration 基本属性
acquireIncrement 当连接池连接用完了,根据该属性决定一次性新建多少连接
initialPoolSize 初始化一次性创建多少个连接
maxPoolSize 最大连接数
maxIdleTime 最大空闲时间,当连接池中连接经过一段时间没有使用,根据该数据进行释放
minPoolSize 最小连接池尺寸

当创建连接池时,一次性创建initialPoolSize 个连接,当连接使用完一次性创建 acquireIncrement 个连接,连接最大数量 maxPoolSize ,当连接池连接数量大于 minPoolSize ,经过maxIdleTime 连接没有使用, 该连接将被释放

ComboPooledDataSource dataSource = new ComboPooledDataSource(“自定义配置名称”);
* 如果不存在配置,将使用默认配置

// 使用c3p0配置文件
        // ComboPooledDataSource dataSource = new ComboPooledDataSource();
         //使用默认
        ComboPooledDataSource dataSource = new ComboPooledDataSource("qust");
        // 自动加载src/c3p0-config.xml
// 手动设置四个参数
    //  dataSource.setDriverClass("com.mysql.jdbc.Driver");
    //  dataSource.setJdbcUrl("jdbc:mysql:///day");
    //  dataSource.setUser("root");
    //  dataSource.setPassword("12345678");
        Connection conn = dataSource.getConnection();
        String sql = "select * from account";
        PreparedStatement stmt = conn.prepareStatement(sql);

        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }

        JDBCUtils.release(rs, stmt, conn);
    }

3.tomcat内置连接池
JNDI原理;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值