C3P0连接池的使用

  • 什么是连接池?
  • C3P0连接池的使用
    • 什么是C3P0?
    • 环境准备
    • 手动配置
      • 主要步骤
      • 开始编写!
    • 采用配置文件的方式
      • 主要步骤
      • 开始编写!
  • 完整代码及运行结果

什么是连接池?

既然叫做池,那么连接池(Connection Pool)就是存在很多连接的一个“池塘”。它是一种创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。简单来说,连接池就是预先准备好了一些连接对象,当有需要的时候就可以直接用这些已经准备好的连接,无需重新创建连接,在有大量连接需求的场合非常适用

如下图,在传统的JDBC连接中,每一次请求访问数据库都要重新创建一个连接来连接数据库。

当我们使用连接池技术后,我们会在连接池中放入足够的待使用的连接。打个比方,有一间屋子(数据库),我们如果要进入这个屋子就需要向主人索要钥匙(数据库连接),一两个人主人可以接受,但要是想要进入这间屋子的人一多,主人就显得比较麻烦了,每次都需要给这些人重新配一把钥匙。于是主人为了方便,就在屋子外放了一个盒子(连接池),里面放了好几把提前配好的钥匙供他人使用。这样一来主人就不会那么麻烦了。

C3P0连接池的使用

什么是C3P0?

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。

环境准备

  • 编译环境:Intelli IDEA
  • 数据库:Mysql 5.7.28
  • C3P0驱动包:c3p0-0.9.1.2.jar
  • Mysql驱动包:mysql-connector-java-5.1.39-bin.jar

驱动包下载地址:https://pan.baidu.com/s/1IeI1r2wFjaxQntu7kS09iA
密码: u086

  • jar包安装过程:详见JDBC入门程序详解
  • 准备数据库数据

手动配置

主要步骤

  • 创建连接池
  • 设置连接参数
  • 从连接池中获得连接

开始编写!

1.定义三个基础变量

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

2.创建连接池

ComboPooledDataSource dataSource = new ComboPooledDataSource();

C3P0的核心类就是ComboPooledDataSource,我们首先将它实例化,就可以调用该类中用于连接数据库的方法。
 
3.设置连接参数

dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/web_test4?useSSL=false");
dataSource.setUser("root");
dataSource.setPassword("Zjy9201141217");
  • setDriverClass用于加载mysql的驱动,作用与Class.forName("com.mysql.jdbc.Driver");相同。
  • 剩下的三个方法setJdbcUrlsetUsersetPassword中所填的参数与DriverManager.getConnection(url,username,password)所需的三个参数相同,只不过分开填写了,这三个参数的具体的解释详见JDBC入门程序详解。

4.从连接池中获得连接

conn=dataSource.getConnection();

ComboPooledDataSource类中有不带参数的getConnection()方法,执行这一语句后开始向连接池中获取已有的连接。

采用配置文件的方式

主要步骤

  • 编写 c3p0-config.xml 文件
  • 创建连接池,默认去类路径查找c3p0-config.xml
  • 从连接池中获得连接

开始编写!

在主要步骤中我们看到了一个新东西-----c3p0-config.xml,这是个什么东西?下面是C3P0的文档:

Named and Per-User configuration: Overriding c3p0 defaults via c3p0-config.xmlGo To Top.As of c3p0-0.9.1, you can define multiple configurations in an XML configuration file, and specify in your code which configuration to use. For any configurations (including the unnamed default configuration), you can define overrides for a particular database user. For example, if several applications access your database under different authentication credentials, you might define maxPoolSize to be 100 for user highVolumeApp, but only 10 for user lowLoadApp. (Recall that Connections associated with different authentication credentials are of necessity separated into separate pools, so it makes sense that these could be configured separately.)You can use the XML config file for all c3p0 configuration, including configuration of defaults. However, for users who don’t want or need the extra complexity, the c3p0.properties file will continue to be supported.By default, c3p0 will look for an XML configuration file in its classloader’s resource path under the name “/c3p0-config.xml”. That means the XML file should be placed in a directly or jar file directly named in your applications CLASSPATH, in WEB-INF/classes, or some similar location.If you prefer not to bundle your configuration with your code, you can specify an ordinary filesystem location for c3p0’s configuration file via the system property com.mchange.v2.c3p0.cfg.xml.

大概就是说,从c3p0-0.9.1开始,我们可以不用在每一次连接中去配置参数,而是可以在c3p0-config.xml中先配置好相关的参数,知道后C3P0可以在类路径查找c3p0-config.xml以获取相关的参数。那么这个xml文件应该放在哪里呢?文档中有这样一句话:

That means the XML file should be placed in a directly or jar file directly named in your applications CLASSPATH, in WEB-INF/classes, or some similar location.

所以我们可以直接将xml文件放到src目录下。下面我们看一下c3p0-config.xml中的代码:

<?xml version="1.0" encoding="UTF-8" ?><c3p0-config><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/web_test4?useSSL=false</property><property name="user">root</property><property name="password">Zjy9201141217</property></default-config><!-- This app is massive! --><named-config name="intergalactoApp"><property name="acquireIncrement">50</property><property name="initialPoolSize">100</property><property name="minPoolSize">50</property><property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching --><property name="maxStatements">0</property><property name="maxStatementsPerConnection">5</property><!-- he's important, but there's only one of him --><user-overrides user="master-of-the-universe"><property name="acquireIncrement">1</property><property name="initialPoolSize">1</property><property name="minPoolSize">1</property><property name="maxPoolSize">5</property><property name="maxStatementsPerConnection">50</property></user-overrides></named-config>
</c3p0-config>

「 解释一下xml文件中的部分标签 」

  • <default-config>标签用来编写默认数据库的具体参数
  • <name-config>标签下用来编写备用的数据库
  • <property>中则是具体的参数值,如url,username,password等
  • initialPoolSize初始化时获取连接数,取值应在minPoolSize与maxPoolSize之间。默认值是3.
  • minPoolSize连接池中保留的最小连接数。默认为:3
  • maxPoolSize连接池中保留的最大连接数。默认为:15
  • maxIdleTime最大空闲时间,多少秒内未使用则连接被丢弃。若为0则永不丢弃。默认值: 0
  • maxStatementsc3p0全局的PreparedStatements缓存的大小。如果maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。如果默认值: 0

配置完xml后,java代码就可以这样写:

//创建连接池默认去类路径查找c3p0-config.xml
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//从连接池中获得连接
conn=dataSource.getConnection();

如果需要用备用的数据库,那吗就在ComboPooledDataSource()的中写上<name-config name="oracle">的name(oracle),ComboPooledDataSource dataSource = new ComboPooledDataSource("oracle");就可以加载相应的数据库。如果ComboPooledDataSource(...)找不到相应的备用数据库,那么就启用默认的数据库。

完整代码及运行结果


public class JDBCUtils {public static void release(ResultSet rs,Statement stmt,Connection conn) {if(stmt!=null) {try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}stmt=null;}if(conn!=null) {try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}conn=null;}if(rs!=null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}rs=null;}}
}
}

import com.itheima.jdbc.utils.JDBCUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;/*** C3P0连接池的测试*/public class C3P0Demo1 {@Test/*** 配置文件方式*/public void demo2(){Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;try{//获得连接:从连接池中获得//创建连接池://创建连接池默认去类路径查找c3p0-config.xmlComboPooledDataSource dataSource = new ComboPooledDataSource();//从连接池中获得连接conn=dataSource.getConnection();String sql = "select * from account";//预编译Sqlpstmt=conn.prepareStatement(sql);//执行SQlrs=pstmt.executeQuery();while (rs.next()){System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getDouble("money"));}}catch (Exception e){e.printStackTrace();}finally {JDBCUtils.release(rs,pstmt,conn);}}/*** 手动设置参数的方式*/public void demo1(){Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;try{//获得连接:从连接池中获得//创建连接池ComboPooledDataSource dataSource = new ComboPooledDataSource();//设置连接参数dataSource.setDriverClass("com.mysql.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/web_test4?useSSL=false");dataSource.setUser("root");dataSource.setPassword("Zjy9201141217");//从连接池中获得连接conn=dataSource.getConnection();String sql = "select * from account";//预编译Sqlpstmt=conn.prepareStatement(sql);//执行SQlrs=pstmt.executeQuery();while (rs.next()){System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getDouble("money"));}}catch (Exception e){e.printStackTrace();}finally {JDBCUtils.release(rs,pstmt,conn);}}
}

C3P0连接池的使用相关推荐

  1. c3p0和jdbctemplate配置oracle集群rac,C3P0连接池、DRUID连接池和JdbcTemplate

    目录 一.C3P0连接池 1.C3P0连接池简介 2.常用的配置参数 3.C3P0连接池基本使用 (1)C3P0配置文件 (2)API介绍 4.使用步骤 二.DRUID连接池 1. DRUID简介 2 ...

  2. C3P0连接池、DRUID连接池和JdbcTemplate

    目录 一.C3P0连接池 1.C3P0连接池简介 2.常用的配置参数 3.C3P0连接池基本使用 (1)C3P0配置文件 (2)API介绍 4.使用步骤 二.DRUID连接池 1. DRUID简介 2 ...

  3. Spring+Hibernate+c3p0连接池配置-连接无法释放的问题解决方案

     1.Spring+Hibernate+c3p0连接池配置: <?xml version="1.0" encoding="UTF-8"?> < ...

  4. spring配置c3p0连接池、spring的声明式事务管理

    一.spring配置c3p0连接池: 1.导入maven依赖: <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> & ...

  5. (十二)C3P0连接池使用教程

    一般我们在项目中操作数据库时,都是每次需要操作数据库就建立一个连接,操作完成后释放连接.因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒),连接就会被自动释放掉.而每次新建连接都 ...

  6. Hibernate配置C3P0连接池(在配好基本的hibernate配置下使用)

    拷贝jar包 找到我们的hibernate安装包,在lib目录下找到optional目录,打开c3p0文件,拷贝里面的jar包到eclipse里 写一个测试类,代码入下 public class C3 ...

  7. Hibernate C3P0连接池配置

    本文向大家介绍Hibernate C3P0连接池,可能好多人还不了解Hibernate C3P0连接池,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西. Hibernate自带的连接池算 ...

  8. c3p0连接池的配置和简单使用

    背景 一般我们在项目中操作数据库时,都是每次需要操作数据库就建立一个连接,操作完成后释放连接.因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒),连接就会被自动释放掉.而每次新建 ...

  9. Java文档阅读笔记-C3P0连接池的使用

    这篇博文如何在应用程序中使用和配置C3P0 prom.xml如下: <dependency><groupId>com.mchange</groupId><ar ...

  10. 【知了堂学习笔记】数据库连接池简介,以及Eclipse中C3p0连接池的简单运用

    1.普通的JDBC连接数据库的弊端 普通的JDBC数据库连接使用 DriverManager 来获取,每次向数据库建立连接的时候都要将 Connection 加载到内存中,再验证用户名和密码(得花费0 ...

最新文章

  1. 安全漏洞“心脏出血”继续 原因是“丘比特”
  2. ps拨号服务器原理_呼叫中心的原理和功能
  3. 【Redis】18.缓存预热、缓存雪崩、缓存击穿、缓存穿透、性能指标监控等企业级解决方案
  4. sublime unable to save 没有那个文件或者目录
  5. Oracle数据库更新时间的SQL语句
  6. tensorflow:双线性插值反卷积
  7. stringreader_Java StringReader markSupported()方法与示例
  8. java invoke 泛型_利用Java反射机制和泛型,全自动解析json
  9. STM32时钟学习之STM3210X_RCC.H解读
  10. c++ qt获取电脑的内存_QT开发(十四)——QT绘图系统
  11. 微信小程序wx.request请求服务器json数据并渲染到页面
  12. Flume+Kafka+Spark Streaming+MySQL实时日志分析
  13. Inter core i7处理器中(x86架构)驱动开发:关于APCI Hardware ID的描述
  14. 旋转变换(二)欧拉角
  15. Linux如何打开U盘
  16. 禾穗HERS | 职场新人第一定律
  17. MX25上SD卡的插拨检测机制
  18. MATLAB---制作动画并演示
  19. 咸鱼CAD笔记—精准绘图
  20. spring 的@PersistenceUnit和@PersistenceContext

热门文章

  1. visio设置网络拓扑图
  2. 关于各种考勤打卡软件破解的思路和比较
  3. 下载pdf分页和不分页代码
  4. ad13批量安装元件库_常用的Altium Designer AD09 AD14 AD18元件库 原理图库(543个)+PCB封装库(509个)...
  5. Javaweb家政服务管理系统的设计与实现
  6. 工程伦理2021秋期末考答案|网课期末考答案|学堂在线|清华大学李正风教授
  7. 自制VBS自动刷屏器,再也不怕刷屏刷不过别人了
  8. vs2010断点调试详细教程
  9. 一阶倒立摆的起摆与稳摆simulink仿真
  10. NSACE|网络信息安全技术,你不能忽视的存在