连接池

一、DBCP连接池

DBCP(DataBase Connection Pool),[数据库连接池]。是 apache 上的一个 java 连接池项目,也是 tomcat 使用的连接池组件。
单独使用dbcp需要2个包:【commons-dbcp.jar,commons-pool.jar】由于建立数据库连接是一个非常耗时的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,用完后再放回去。

使用方式

  1. 创建项目
  2. 导入相应jar包:commons-dbcp.jar、commons-pool.jar、commons-logging.jar 日志支持
  3. properties配置文件解析处理

    文件名称: dbcp.properties,放到src目录下

#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/practice?useSSL=true&characterEncoding=utf8
username=root
password=123456
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxTotal=50
#<!-- 最大空闲连接 -->
maxIdle=20
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位,总访问量超过连接数量,则等待,超时后不再等待 -->
maxWaitMillis=5000
  1. 代码处理
/**
* DBUtils_DBCP工具类
*/
public class DBUtils_DBCP {
    private static BasicDataSource dataSource;
    static {
        try {
            Properties properties = new Properties();
            //类加载器获取properties文件
            InputStream is = DBUtils_DBCP.class.getClassLoader().getResourceAsStream("dbcp.properties");
            properties.load(is);
            //调取数据源工厂,并使用创建数据源的方法,将properties文件导入
            dataSource = BasicDataSourceFactory.createDataSource(properties);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //对外提供获取连接的方法
    public static Connection getConnections() {
        try {
            Connection connection = dataSource.getConnection();
            return connection;
        } catch (SQLException e) {
            System.out.println("获取失败");
            e.printStackTrace();
        }
        return null;
    }
}

/**
* 测试
*/
@Test
public void testDBCP() {
    for (int i = 0; i < 100; i++) {
        Connection connection = DBUtils_DBCP.getConnections();
        System.out.println("获取了:" + connection.hashCode());
        try {
            connection.close(); //回收,并没有关闭,而是将连接放回池中
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

二、C3P0连接池

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目有Hibernate,Spring等。

DBCPC3P0
dbcp没有自动回收空闲连接的功能c3p0有自动回收空闲连接功能
dbcp需要手动加载配置文件c3p0自动加载

使用方式

  1. 创建项目

  2. 导入jar包

    c3p0-0.9.1.2.jar
    mchange-commons-java-0.2.11.jar
    mysql驱动包

  3. 添加配置文件

    c3p0是在外部添加配置文件,工具直接进行应用,因为直接引用,所以要求固定的命名和文件位置
    文件位置: src
    文件命名:c3p0-config.xml / c3p0.properties,名字不能出错

    XML文件

<c3p0-config>
	<!-- 默认配置,如果没有指定则使用这个配置 -->
	<default-config>
		<!-- 基本配置 -->
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/school</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<!--扩展配置-->
		<!-- 连接超过10秒报错-->
		<property name="checkoutTimeout">10000</property>
		<!--30秒检查空闲连接 -->
		<property name="idleConnectionTestPeriod">30</property>
		<!-- 初始大小 -->
		<property name="initialPoolSize">10</property>
		<!-- 每次增长的个数 -->
		<property name="acquireIncrement">5</property>
		 <!-- 30秒不适用丢弃-->
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">50</property>
		<property name="minPoolSize">5</property>
	</default-config> 
	<!-- 命名的配置 -->
	<named-config name="bj1805">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/day2</property>
		<property name="user">root</property>
		<property name="password">111</property>
		<!-- 如果池中数据连接不够时一次增长多少个 -->
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">20</property>
		<property name="minPoolSize">10</property>
        <property name="maxPoolSize">40</property>
	</named-config>
</c3p0-config> 

properties文件

c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/practice?useSSL=true&characterEncoding=utf8&rewriteBatchedStatements=true
c3p0.user=root
c3p0.password=123456
c3p0.acquireIncrement=5
c3p0.initialPoolSize=20
c3p0.minPoolSize=10
c3p0.maxPoolSize=50
  1. 代码处理
/**
* DBUtils_C3p0工具类
*/
public class DBUtils_C3p0 {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    public static Connection getConnections() {
        try {
            Connection connection = dataSource.getConnection();
            return connection;
        } catch (SQLException e) {
            System.out.println("获取失败");
            e.printStackTrace();
        }
        return null;
    }
}

/**
* 测试
*/
@Test
public void testC3p0() {
    for (int i = 0; i < 100; i++) {
        Connection connection = DBUtils_C3p0.getConnections();
        System.out.println("获取了" + i + connection.hashCode());
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

注意事项

  1. c3p0的配置文件内部可以包含命名配置文件和默认配置文件!默认是选择默认配置!如果需要切换命名配置可以在创建c3p0连接池的时候填入命名即可!
  2. 如果xml配置文件和属性文件都存在时,xml优先级高于属性文件

三、Druid连接池【重要】

Druid 是目前比较流行的高性能的,分布式列存储的OLAP框架(具体来说是MOLAP)。它有如下几个特点:
一. 亚秒级查询
druid提供了快速的聚合能力以及亚秒级的OLAP查询能力,多租户的设计,是面向用户分析应用的理想方式。

二.实时数据注入
druid支持流数据的注入,并提供了数据的事件驱动,保证在实时和离线环境下事件的实效性和统一性

三.可扩展的PB级存储
druid集群可以很方便的扩容到PB的数据量,每秒百万级别的数据注入。即便在加大数据规模的情况下,也能保证时其效性

四.多环境部署
druid既可以运行在商业的硬件上,也可以运行在云上。它可以从多种数据系统中注入数据,包括hadoop,spark,kafka,storm和samza等

五.丰富的社区
druid拥有丰富的社区,供大家学习

使用步骤

  1. 创建项目
  2. 导入jar包:【druid-1.1.5.jar】和 【mysql包】
  3. 配置文件 database.properties
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/practice?useSSL=true&characterEncoding=utf8
username=root
password=123456
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxActive=50
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=5000
  1. 代码处理
/**
* DBUtils_druid工具类
*/
public class DBUtils_druid {
    private static DruidDataSource dataSource;
    static {
        try {
            Properties properties = new Properties();
            InputStream is = DBUtils_druid.class.getClassLoader().getResourceAsStream("database.properties");
            properties.load(is);
            //同样需要数据源工厂的创建数据源方法,导入properties文件
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnections() {
        try {
            Connection connection = dataSource.getConnection();
            return connection;
        } catch (SQLException e) {
            System.out.println("获取失败");
            e.printStackTrace();
        }
        return null;
    }
}

/**
* 测试
*/
@Test
public void testDruid() {
    for (int i = 0; i < 100; i++) {
        Connection connection = DBUtils_druid.getConnections();
        System.out.println("获取了" + i + connection.hashCode());
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值