连接池 —— DBCP技术 和 C3P0 技术

作者: Mr_欢先生 | 来源:发表于2017-05-29 16:14 被阅读194次

一.概述

Sun公司约定: 如果是连接池技术,需要实现一个接口!

javax.sql.DataSource;

其他的开源组织提供了数据源的独立实现

二.DBCP技术使用

1.Apache:DBCP技术

  • DBCP是 Apache 软件基金组织下的开源连接池实现,使用DBCP数据源,应用程序应在系统中增加如下两个 jar 文件:
    Commons-dbcp.jar:连接池的实现
    Commons-pool.jar:连接池实现的依赖库
    包下载链接:传送门密码:nshu
    maven环境配置:
<dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.6</version>
    </dependency>
  • Tomcat 的连接池正是采用该连接池来实现的。该数据库连接池既可以与应用服务器整合使用,也可由应用程序独立使用。
    核心类:BasicDataSource
  • 使用步骤
    引入jar文件
    --commons-dbcp-1.4.jar
    --commons-pool-1.5.6.jar
1.硬编码方式实现连接池
package com.huan.car.dao.poolDBCP;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by 马欢欢 on 2017/5/29.
 */
public class PoolDBCP {
    @Test
    public void testDbcp() throws SQLException {
        String url="jdbc:mysql://localhost:3306/car";//2.指定连接数据库的地址名称
        String user="root";
        String password = "root";//指定用户名和密码
        PreparedStatement pstmt;
        //连接池核心类
        BasicDataSource dataSource = new BasicDataSource();
        //连接池参数配置:初始化连接数,最大连接数
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");//驱动
        dataSource.setUrl(url);//连接字符串
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        dataSource.setInitialSize(3);//初始化连接数
        dataSource.setMaxActive(6);//最大连接数
        dataSource.setMaxIdle(3000);//最大空闲时间

        //获取连接
        Connection conn = dataSource.getConnection();
        pstmt = conn.prepareStatement(" delete from car where id= ? ");
        pstmt.setInt(1,10);
        pstmt.executeUpdate();
        //关闭
        conn.close();
    }
}

删除成功
2.配置方式实现连接池
  • 1.配置文件db.properties:

db.properties(注意当报错NullPointerException时,需要将文件添加到同级的classes文件中)配置文件中的key与BaseDataSouce属性一样

url=jdbc:mysql://localhost:3306/car
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
initialSize=3
maxActive=6
maxIdle=3000
需要将文件添加到同级的classes文件中
  • 2.连接代码
  @Test
    public void testProp()throws Exception{
        PreparedStatement pstmt;
//        加载prop配置文件
        Properties prop = new Properties();
        //获取文件流
        InputStream inStream = PoolDBCP.class.getResourceAsStream( "db.properties");
        //加载属性配置文件
        prop.load(inStream);
        //根据prop配置文件直接创建数据源对象
        DataSource dataSource = BasicDataSourceFactory.createDataSource(prop);
        //获取连接
        Connection conn = dataSource.getConnection();
        pstmt = conn.prepareStatement(" delete from car where id= ? ");
        pstmt.setInt(1,11);
        pstmt.executeUpdate();
        //关闭
        conn.close();


    }
删除成功

三.C3P0技术使用[最常用的连接池技术]

C3P0连接池技术:

  • 最常用的连接池技术!Spring框架,默认支持C3P0连接池技术!
    C3P0连接池,核心类:
    CombopooledDataSource ds;
    使用:
  1. 下载,引入jar文件: c3p0-0.9.1.2.jar
    包下载链接:链接:c3p0-0.9.1.2.jar 密码:2bxo
    maven环境配置:
<dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
  1. 使用连接池,创建连接
    a) 硬编码方式
    b) 配置方式(xml)[需要将文件添加到classes文件中的根目录下]
1.硬编码方式,使用C3P0
   //1.硬编码方式,使用C3P0
    @Test
    public void testCode() throws PropertyVetoException, SQLException {
        String url="jdbc:mysql://localhost:3306/car";//2.指定连接数据库的地址名称
        String user="root";
        String password = "root";//指定用户名和密码
        PreparedStatement pstmt;
        //创建连接池核心工具类
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //连接池参数配置:初始化连接数,最大连接数
        dataSource.setJdbcUrl(url);
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setInitialPoolSize(3);
        dataSource.setMaxPoolSize(6);
        dataSource.setMaxIdleTime(1000);

        //从连接池对象中获取连接对象
        Connection conn = dataSource.getConnection();
        pstmt = conn.prepareStatement(" delete from car where id= ? ");
        pstmt.setInt(1,11);
        pstmt.executeUpdate();
        //关闭
        conn.close();

    }
2.配置方式,使用C3P0[重要]
  • c3p0-config.xml
<c3p0-config>
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/car</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">3</property>
        <property name="maxPoolSize">6</property>
        <property name="maxIdleTime">1000</property>
        <!--<property name="checkoutTimeout">30000</property>-->
        <!--<property name="maxIdleTimeExcessConnections">10</property>-->
        <!--<property name="maxConnectionAge">60</property>-->
        <!--<property name="propertyCycle">1</property>-->
        <!--<property name="minPoolSize">5</property>-->
        <!--<property name="maxStatements">0</property>-->
        <!--<property name="maxStatementsPerConnection">5</property>-->
        <!--<property name="maxAdministrativeTaskTime">4</property>-->
        <!--<property name="connectionCustomizerClassName">com.mchange.v2.c3p0.test.TestConnectionCustomizer</property>-->
        <!--<property name="unreturnedConnectionTimeout">15</property>-->
        <!--<property name="debugUnreturnedConnectionStackTraces">true</property>-->

        <!--
        <user-overrides user="swaldman">
          <property name="debugUnreturnedConnectionStackTraces">true</property>
        </user-overrides>
        -->

    </default-config>

    <!--
      <named-config name="dumbTestConfig">
        <property name="maxStatements">200</property>
        <property name="jdbcUrl">jdbc:test</property>
        <user-overrides user="poop">
          <property name="maxStatements">300</property>
        </user-overrides>
       </named-config>
    -->

</c3p0-config>

![需要将文件添加到classes文件中的根目录下]](http:https://img.haomeiwen.com/i1616232/d65c0e47f27b605a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

  • 源码
  //2.配置方式,使用C3P0
    @Test
    public void testXml() throws SQLException {
        PreparedStatement pstmt;
        //创建连接池核心工具类
        //自动加载根目录下面的c3p0配置文件【c3p0-config.xml】
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //从连接池对象中获取连接对象
        Connection conn = dataSource.getConnection();
        pstmt = conn.prepareStatement(" delete from car where id= ? ");
        pstmt.setInt(1,20);
        pstmt.executeUpdate();
        //关闭
        conn.close();

    }
  • 结果


    image.png

文章文集:JavaEE--学习笔记

相关文章

网友评论

    本文标题:连接池 —— DBCP技术 和 C3P0 技术

    本文链接:https://www.haomeiwen.com/subject/ebskfxtx.html