数据库连接池
数据库连接—执行完毕—释放
连接—释放 十分浪费资源
池化技术:准备一些预先的资源,过来就连接预先准备好的
-------开门—业务员:等待–服务—
最小连接数: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;characterEncoding=utf8&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;characterEncoding=utf8&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();
}
}
}
}
测试