连接池学习
一、为什么需要连接池在传统的JDBC开发中每次操作数据库都需要1.加载驱动 2.创建连接 3.执行SQL 4.关闭连接缺点需要自己创建连接和销毁连接这样是比较消耗时间资源等有一些连接池已经创建好了一些连接现在可以从连接池中获取到这样就节省创建连接时间直接使用这些连接归还到连接池中。优点节省创建连接与释放连接性能消耗连接池中连接起到复用的作用提升程序性能二、连接池参数池参数如果不指定有默认值初始大小10个最小空闲连接数3个增量一次创建的最小单位5个最大空闲连接数12个最大连接数20个最大的等待时间1000毫秒三、4个参数任何的开源的连接池4大参数都需要自己来设置* 驱动的名称‐‐ com.mysql.jdbc.Driver* 连接‐‐ jdbc:mysql:///day14* 用户名‐‐ root* 密码‐‐ root四、DataSource 接口SUN 公司定义了javax.sql.DataSource接口所有连接池都必须实现它public interface DataSource { Connection getConnection() throws SQLException; }开源连接池DBCP、C3P0、Druid都实现了该接口通过getConnection()获取连接调用close()方法时底层被增强为归还连接而非销毁五、主流连接池对比连接池特点DBCPApache 提供轻量级C3P0支持自动回收空闲连接Druid阿里出品功能最强支持监控、SQL日志、加密六、Druid 连接池实战1. 导入依赖dependency groupIdcom.alibaba/groupId artifactIddruid/artifactId version1.1.10/version /dependency2.编写测试程序import com.alibaba.druid.pool.DruidDataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; /** * 测试连接池对象 */ public class JdbcTest6 { public static void main(String[] args) { // 创建连接池对象从连接池中获取到连接对象 DruidDataSource dataSource new DruidDataSource(); // 设置4个参数 驱动类 地址 用户名 密码 dataSource.setDriverClassName(com.mysql.jdbc.Driver); dataSource.setUrl(jdbc:mysql:///jdbcdemo); dataSource.setUsername(root); dataSource.setPassword(root); // 设置初始化连接个数默认是0 dataSource.setInitialSize(5); // 设置最大连接数 dataSource.setMaxActive(10); // 最大等待时间单位是毫秒 2秒 dataSource.setMaxWait(2000); // 定义链接对象 Connection conn null; PreparedStatement stmt null; try { // 获取到连接对象 conn dataSource.getConnection(); // 编写SQL语句 String sql insert into t_user values (null,?,?,?); // 预编译SQL语句 stmt conn.prepareStatement(sql); // 设置值 stmt.setString(1,eee); stmt.setString(2,eee); stmt.setString(3,eee); // 执行sql stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { // 释放资源 if(stmt ! null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn ! null){ try { // 把conn关闭了其实连接池的底层已经对close方法进行增强。原来是销毁连接现在是归还连接。 conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }3. 配置文件方式druid.propertiesdriverClassNamecom.mysql.jdbc.Driver urljdbc:mysql:///jdbcdemo usernameroot passwordroot initialSize5 maxActive10 maxWait3000 maxIdle6 minIdle34.编写工具类import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.InputStream; import java.sql.*; import java.util.Properties; /** * JDBC的工具类 1.0版本 * JDBC的工具类 2.0版本智能一些编写properties属性文件程序就可以读取属性文件 * JDBC的工具类 3.0版本加入连接池对象 */ public class JdbcUtils2 { // 连接池对象 private static DataSource DATA_SOURCE; static{ // 加载属性文件 Properties pro new Properties(); InputStream inputStream JdbcUtils2.class.getResourceAsStream(/druid.properties); try { // 加载属性文件 pro.load(inputStream); // 创建连接池对象 DATA_SOURCE DruidDataSourceFactory.createDataSource(pro); } catch (Exception e) { e.printStackTrace(); } } /** * 从连接池中获取连接返回。 * return */ public static Connection getConnection(){ Connection conn null; try { conn DATA_SOURCE.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return conn; } /** * 关闭资源 * param conn * param stmt * param rs */ public static void close(Connection conn, Statement stmt, ResultSet rs){ if(rs ! null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if(stmt ! null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn ! null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭资源 * param conn * param stmt */ public static void close(Connection conn, Statement stmt){ if(stmt ! null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn ! null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }