1.1 C3p0连接池的简介

  • C3P0地址:https://sourceforge.net/projects/c3p0/?source=navbar C3P0是一个开源的连接池。Hibernate框架,默认推荐使用C3P0作为连接池实现。 C3P0的jar包:c3p0-0.9.1.2.jar

C3P0连接池jar包(完整版)百度网盘链接:https://pan.baidu.com/s/1dAOVaZJP9DxXh81kgZ-HbA 密码:u711

1.2 常用的配置参数

参数 说明
initialPoolSize  初始连接数
maxPoolSize 最大连接数
checkoutTimeout 最大等待时间
maxIdleTime 最大空闲回收时间
  1. 初始连接数:刚创建好连接池的时候准备的连接数量
  2. 最大连接数:连接池中最多可以放多少个连接
  3. 最大等待时间:连接池中没有连接时最长等待时间
  4. 最大空闲回收时间:连接池中的空闲连接多久没有使用就会回收

2.1  C3p0的基本使用

  1. 拷贝c3p0的jar到lib目录  (c3p0-0.9.5.2.jar , mchange-commons-java-0.2.12.jar)
  2. 把配置文件c3p0.xmla拷贝到src目录下(配置文件名一定不能修改,而且必须拷贝到src目录下
  3. 创建c3p0连接池.
  • ComboPooledDataSource核心类:
  • getConnection();
package C3p0;import com.mchange.v2.c3p0.ComboPooledDataSource;import java.sql.Connection;
import java.sql.SQLException;public class Demo01 {public static void main(String[] args) throws SQLException {//C3p0的一个类ComboPooledDataSource DataSource = new ComboPooledDataSource();System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());Connection connection = DataSource.getConnection();System.out.println("连接:"+connection);connection.close();System.out.println("连接:"+DataSource.getConnection());}
}

2.1.1使用指定配置文件名字的C3p0:

<named-config name="otherc3p0"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/mysql</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">5</property><property name="maxPoolSize">10</property><property name="checkoutTimeout">3000</property></named-config>
package C3p0;import com.mchange.v2.c3p0.ComboPooledDataSource;import java.sql.Connection;
import java.sql.SQLException;public class Demo01 {public static void main(String[] args) throws SQLException {//C3p0的一个类ComboPooledDataSource DataSource = new ComboPooledDataSource("otherc3p0");System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());System.out.println("连接:"+DataSource.getConnection());Connection connection = DataSource.getConnection();System.out.println("连接:"+connection);connection.close();System.out.println("连接:"+DataSource.getConnection());}
}

  • 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/test</property><property name="user">root</property><property name="password">root</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/mysql</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">5</property><property name="maxPoolSize">10</property><property name="checkoutTimeout">3000</property></named-config>
</c3p0-config>

更多资料,搜索或扫码关注公众号:数说Cloud

C3P0连接池的配置和使用相关推荐

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

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

  2. C3P0连接池详细配置

    转自:http://blog.csdn.net/xiamizy/article/details/2030214 <c3p0-config> <default-config> & ...

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

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

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

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

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

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

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

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

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

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

  8. Hibernate C3P0连接池配置

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

  9. hibernate4配置c3p0连接池报错

    在hibernate的xml文件中配置c3p0连接池时,运行报错: java.lang.ClassNotFoundException: Could not load requested class : ...

  10. MyBatis配置C3P0连接池

    这两天学到Mybatis感觉就要疯了,第一次接触,我是不是应该写点笔记,下面呢,记录一下连接池的配置,防止忘了. 第一步,二话不说,先导入所需jar包(如图所示三个) 第二步,继承UnpooledDa ...

最新文章

  1. SAP MM MI01事务代码里的批次确定
  2. VMWare 虚拟机启动报“内部错误”的解决办法
  3. Python科学计算包应用-教你以可视化的方式打开NumPy
  4. 使用三目运算嵌套方法 或 临时变量方法: 获取三个整数中最大值的数
  5. Ajax与三层架构实训教案
  6. 链接聚合是将一组物理接口_500字描述华为VLAN聚合工作原理 你看懂了吗?
  7. SLAM GMapping(5)运动模型
  8. java web w3c_1.3 搭建Java Web开发环境
  9. 怎么在谷歌浏览器中安装.crx扩展名的离线Chrome插件?
  10. c java 字节流_Java 学习(23)---(IO流之字节流)
  11. access无法与wincc链接_step7与s7-300 PLC连接,但是wincc与PLC连接不上怎么办?
  12. 博弈论个人的一点小总结
  13. 计算机控制系统的数字量输出通道由,计算机控制-习题
  14. 华硕双屏笔记本windows系统驱动安装
  15. 区块链 交易和区块数据存在哪儿
  16. 《机器学习算法竞赛实践》学习笔记(1)神经网络
  17. mysql 存储过程
  18. windows通过iscsi挂载linux硬盘
  19. LabVIEW 2018 下载、安装、环境搭建及破解
  20. 物联网概论(IoT)__Chp4 传感器与无线传感网//WSN

热门文章

  1. 威伦触摸屏脚本,宏指令
  2. 给大家推荐一套 git 工作流
  3. 机器人控制算法----模糊控制
  4. ##智能优化算法复习--免疫算法IA
  5. android 屏幕点击录制视频教程,安卓手机怎么屏幕录像,手机屏幕录像详细教程...
  6. pandas官方中文手册pdf下载
  7. STM32标准外设库(标准库)官网下载方法,附带2021最新标准固件库下载链接
  8. oracle改字符集sjis,MySQL字符集专题(字符集,校对,乱码)_MySQL
  9. Java实现微信刷屏(2)
  10. RS232 DB9 公头 母头 串口引脚定义