数据库连接池

数据库连接池

数据库连接—执行完毕—释放

连接—释放 十分浪费资源

池化技术:准备一些预先的资源,过来就连接预先准备好的

-------开门—业务员:等待–服务—

最小连接数:10

最大连接数:15

等待超时:100ms

编写连接池,实现一个接口 DataSource

开源数据源实现

DBCP

C3P0

Druid: 阿里巴巴

使用了这些数据库之后,我们在项目开发中就不需要编写连接数据库的代码了!

DBCP

需要用到的jar包

commons-dbcp-1.4 , commons-pool-1.6

DBCP配置文件:

#连接设置  
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbctest?useUnicode=true&characterEncoding=utf8&useSSL=true"
username=root
password=234567
#<!-- 初始化连接 -->
initialSize=10
#<最大连接数量
maxTotal=50
#<!--最大空闲连接 -->
maxIdle=20
#<!--最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位
maxWaitMillis=6000

URL解析

url=jdbc:mysql://localhost:3306/jdbctest?
useUnicode=true&characterEncoding=utf8&useSSL=true"
// useUnicode=true -> 支持中文
// characterEncoding=utf8 -> 设置字符节编码为utf8
// useSSL=true -> 使用安全连接

//协议://主机地址:端口号/数据库名?参数1&参数2&参数3

提取DBCP工具类

public class JdbcUtils_DBCP {
private static DataSource dataSource = null;
	static {
        try {
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);
            //创建数据源  工厂模式-->创建
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();//从数据源获取连接
    }·
    //释放资源
    public static void release(Connection con, Statement st, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

测试
在这里插入图片描述

在这里插入图片描述

C3P0

需要用到的jar包

c3p0-0.9.5.5 ,machange-commons-java-0.2.19

C3P0配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

    <!-- c3p0默认配置,下面还可以配置多个数据库 -->
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbctest?useUnicode=true&amp;amp;characterEncoding=utf8&amp;amp;useSSL=true"
        </property>
        <property name="user">root</property>
        <property name="password">234567</property>
        <!-- 初始化时获取连接数 -->
        <property name="initialPoolSize">10</property>
        <!-- 连接池中保留的最大连接数 -->
        <property name="maxPoolSize">20</property>
        <!-- 最大空闲时间,多少秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 -->
        <property name="maxIdleTime">1000</property>
        <property name="acquireIncrement">5</property>
    </default-config>

    <named-config name="mysql">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbctest?useUnicode=true&amp;amp;characterEncoding=utf8&amp;amp;useSSL=true"
        </property>
        <property name="user">root</property>
        <property name="password">234567</property>
        <!-- 初始化时获取连接数 -->
        <property name="initialPoolSize">10</property>
        <!-- 连接池中保留的最大连接数 -->
        <property name="maxPoolSize">20</property>
        <!-- 最大空闲时间,多少秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0 -->
        <property name="maxIdleTime">1000</property>
        <property name="acquireIncrement">5</property>
    </named-config>
</c3p0-config>

在这里插入图片描述

提取C3P0的工具类

public class JdbcUtils_C3P0 {
    private static ComboPooledDataSource dataSource = null;

    static {
        try {
            //创建数据源
            dataSource = new ComboPooledDataSource();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();//从数据源获取连接
    }

    //释放资源
    public static void release(Connection con, Statement st, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

测试
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值