数据库连接池的概念
1 数据库连接的背景
- 数据库的连接是一种关键的,优先的,昂贵的资源,在多用户的网页应用程序中体现的尤为重要,
- 对数据库连接的管理能显著影响到整个应用程序的性能指标
2 数据库连接池
-
数据库连接池负责分配,管理和释放数据库连接允许应用程序重复使用一个现有的数据库连接,而不是新建一个.这项技术能显著提升对数据库操作的性能
-
能避免重复创建连接,提高代码执行效率
数据库连接产品
C3P0数据库连接池
-
使用步骤
-
导包 (MySQL驱动包 + C3p0两个jar包)
-
配置文件(文件名称固定c3p0-config.xml)
<c3p0-config> <!-- 使用默认的配置读取连接池对象 --> <default-config> <!-- 连接参数 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://192.168.59.129:3306/db14</property> <property name="user">root</property> <property name="password">itheima</property> <!-- 连接池参数 --> <!--初始化的连接数量--> <property name="initialPoolSize">5</property> <!--最大连接数量--> <property name="maxPoolSize">10</property> <!--超时时间--> <property name="checkoutTimeout">3000</property> </default-config> <!-- 使用其他的配置读取连接池对象 --> <named-config name="otherc3p0"> <!-- 连接参数 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/db15</property> <property name="user">root</property> <property name="password">itheima</property> <!-- 连接池参数 --> <property name="initialPoolSize">5</property> <property name="maxPoolSize">8</property> <property name="checkoutTimeout">1000</property> </named-config> </c3p0-config>
-通过直接创建CombopooledDataSource对象来获取连接
public class C3P0Test1 { public static void main(String[] args) throws Exception{ //1.创建c3p0的数据库连接池对象 DataSource dataSource = new ComboPooledDataSource(); //2.通过连接池对象获取数据库连接 Connection con = dataSource.getConnection(); //3.执行操作 String sql = "SELECT * FROM student"; PreparedStatement pst = con.prepareStatement(sql); //4.执行sql语句,接收结果集 ResultSet rs = pst.executeQuery(); //5.处理结果集 while(rs.next()) { System.out.println(rs.getInt("sid") + "\t" + rs.getString("name") + "\t" + rs.getInt("age") + "\t" + rs.getDate("birthday")); } //6.释放资源 rs.close(); pst.close(); con.close(); } }
-
Druid连接池
-
使用步骤
-
导包(druid包 + Mysql驱动)
-
配置文件编写
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://数据库的ip:端口号(默认是3306)/要连接的数据库名称 username=数据库用户名 password=数据库密码 #初始化池子的数量 initialSize=指定大小 #最大连接池数量 maxSize=最大大小 #超时等待时间 maxwait=指定时间(单位毫秒)
-
-
具体步骤
-
加载配置文件
//获取配置文件的流对象 InputStream is = DruidTest1.class.getClassLoader().getResourceAsStream("druid.properties"); //1.通过Properties集合,加载配置文件 Properties prop = new Properties(); prop.load(is);
-
通过DruidDataSourceFactory获取DataSource
//2.通过Druid连接池工厂类获取数据库连接池对象 DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); //3.通过连接池对象获取数据库连接进行使用 Connection con = dataSource.getConnection();
-
举例
//查询student表中所有的信息 public class DruidTest2 { //导包 //配置文件编写 public static void main(String[] args) { InputStream is = null; Connection conn = null; PreparedStatement pstat = null; ResultSet rs = null; try { //读取配置文件 is = DruidTest2.class.getClassLoader().getResourceAsStream("druid.properties"); Properties properties = new Properties(); properties.load(is); //通过druid连接池工厂获取数据库连接池对象 DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); //通过连接池对象获取数据库进行使用 conn = dataSource.getConnection(); //获取执行者对象 String sql = "SELECT * FROM student"; pstat = conn.prepareStatement(sql); //执行sql语句得到ResultSet结果集 rs = pstat.executeQuery(); //处理结果(处理方式为打印到控制台) while (rs.next()) { System.out.println( rs.getInt("sid") + rs.getString("name") + rs.getInt("age") + rs.getString("birthday") ); } } catch (Exception e) { e.printStackTrace(); //释放资源 } finally { try { if (is != null) { is.close(); } if (conn != null) { conn.close(); } if (pstat != null) { pstat.close(); } if (rs != null) { rs.close(); } } catch (Exception e) { e.printStackTrace(); } } } }
-
策略模式
- 执行者(谁执行策略)
- 策略(策略接口)
- 策略实现1
- 策略实现2
- …
- 调用者
- 创建策略传递给执行者
策略模式详细文章引自: https://blog.csdn.net/wozniakzhang/article/details/90526668
案例(篮球,足球,运动类,运动员类…)帮助回忆
土话理解:
处理结果集resultset有多种方式,创建一个接口,定义一个抽象方法,具体怎么实现解析结果集由他的子类去具体实现,然后在调用的时候明确要是用按个然后传入接口的具体实现类,只有在具体想怎么解析这个结果集的时候才能知道需要怎么解析结果集
一个接口定义一个抽象方法,有多个实现类,实现的功能都不同,在调用的时候,你想怎么实现或者解析他,就在调用的时候传入他具体的实现类
注意事项
1.使用combopooleddatasource()空参构造 使用的是xml的默认配置
2.当连接池连接满了后,不会直接打回冷宫而是在超时时间过后再操作
3.C3P0 的配置文件会自动加载,但是必须叫 c3p0-config.xml 或 c3p0-config.properties
4.druid连接池使用
导入jar包
编写配置文件,放在src下
properties读取配置文件
通过druid连接池工厂类获取数据库连接池
通过连接池对象获取数据库连接进行使用