【Java】JDBC Part5 DataSource 连接池操作
JDBC Part5 DataSource 连接池操作
- javax.sql.DataSource 接口,通常由服务器实现
- DBCP Tomcat自带相对C3P0速度较快,但存在BUG,已经不更新了
- Proxool 没听过、能监控连接池状态,稳定性差
- C3P0 速度较慢,但是稳定
- Druid 阿里巴巴提供,集成上面的所有优点,
- Hikari 目前最快的连接池依赖,据说有安全问题。。。
DataSource被称为数据源,包含连接池和连接池管理2部分
C3P0实现
官方文档: https://blog.csdn.net/wangwei_cq/article/details/8930667
Maven依赖
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
第一种,硬编码的连接池
package connector; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test; import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException; /**
* @author ArkD42
* @file Jdbc
* @create 2020 - 04 - 24 - 17:49
*/
public class C3P0Test { @Test
public void dataSourceByC3p0() throws PropertyVetoException, SQLException {
// 获取池对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
// 配置连接信息
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbc_db?serverTimezone=Asia/Shanghai");
dataSource.setUser("root");
dataSource.setPassword("123456");
//设置初始的连接数
dataSource.setInitialPoolSize(10);
//获取连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
测试结果

第二种 XML配置文件

配置XML配置文件的信息
<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
<!-- 自定义的配置命名-->
<named-config name="c3p0 XML config"> <!-- 四个基本信息 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<!-- 默认本地可以省略 jdbc:mysql:///jdbc_db?serverTimezone=Asia/Shanghai -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_db?serverTimezone=Asia/Shanghai</property>
<property name="user">root</property>
<property name="password">123456</property> <!-- 连接池管理信息 --> <!-- 连接对象数不够时,每次申请 迭增的连接数 -->
<property name="acquireIncrement">5</property>
<!-- 初始池大小存放的连接对象数 -->
<property name="initialPoolSize">10</property>
<!-- 最小连接对象数 -->
<property name="minPoolSize">10</property>
<!-- 最大连接对象数,不可超出的范围 -->
<property name="maxPoolSize">100</property>
<!-- 最多维护的SQL编译对象个数-->
<property name="maxStatements">50</property>
<!-- 每个连接对象最多可使用的SQL编译对象的个数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
注意获取配置名

如果密码错误,其他配置问题,连接池运行一段时间获取不到连接自动超时退出,报异常
封装JdbcForC3P0Util工具类
import java.sql.*;
import java.util.ArrayList;
import java.util.List; /**
* @author ArkD42
* @file Jdbc
* @create 2020 - 04 - 24 - 18:23
*/
public class JdbcForC3p0Util { // 池对象只需要一个即可
private static final ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("c3p0 XML config"); // 获取连接对象,从池对象获取的对象可允许多个
public static Connection getConnection(){
try {
return comboPooledDataSource.getConnection();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return null;
} // 释放资源 对象没有的情况直接null注入
public static void releaseResource(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
try{
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException sqlException){
sqlException.printStackTrace();
}
} // 增删改
public static int update(String sql,Object[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
if (args != null ) for (int i = 0; i < args.length; i++) preparedStatement.setObject(i+1,args[i]);
int i = preparedStatement.executeUpdate();
return i;
} catch (SQLException e) {
e.printStackTrace();
} finally {
releaseResource(connection,preparedStatement,null);
}
return 0;
} // 查询
public static <T> List<T> queryList(Class<T> tClass, String sql, Object[] args){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
if (args != null) for (int i = 0; i < args.length; i++) preparedStatement.setObject(i+1,args[i]);
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
List<T> tList = new ArrayList<T>();
while(resultSet.next()){
T t = tClass.newInstance();
for (int i = 0; i < columnCount; i++) {
Object columnValue = resultSet.getObject(i + 1);
String columnLabel = metaData.getColumnLabel(i + 1);
Field field = tClass.getDeclaredField(columnLabel);
field.setAccessible( true );
field.set(t,columnValue);
}
tList.add(t);
}
return tList;
} catch (Exception e){
e.printStackTrace();
} finally {
releaseResource(connection,preparedStatement,resultSet);
}
return null;
}
}
测试,Blob不可封装为实体类对象,所以大文件的字段我删除了

DBCP 连接池操作
Maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.8.0</version>
</dependency>
连接实现一,硬编码连接
public class DBCP {
@Test
public void dbcp() throws Exception{
// 创建连接池
BasicDataSource dataSource = new BasicDataSource();
// 配置信息
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///jdbc_db?serverTimezone=Asia/Shanghai");
dataSource.setUsername("root");
dataSource.setPassword("123456");
// 连接池管理设置
//dataSource.setInitialSize(10); // 初始化池种的连接对象个数
//dataSource.setMaxIdle(10); // 最大空闲连接对象个数
//dataSource.setMinIdle(2); // 最小空闲连接对象个数
// 获取连接对象
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
测试结果

连接实现二,读取配置文件
dbcp.properties配置文件的信息
# 注意这个driverClassName
driverClassName = com.mysql.cj.jdbc.Driver
url = jdbc:mysql:///jdbc_db?serverTimezone=Asia/Shanghai
# 注意这个username
username = root
password = 123456
测试单元
@Test
public void dbcp2() throws Exception{
InputStream inputStream = DBCP.class.getClassLoader().getResourceAsStream("dbcp.properties");
Properties properties = new Properties();
properties.load(inputStream);
BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
第一种获取方式【通用】
InputStream inputStream = DBCP.class.getClassLoader().getResourceAsStream("dbcp.properties");
第二种获取方式
// Maven工程的读取路径
InputStream inputStream = new FileInputStream(new File("src/main/resources/dbcp.properties")); // 普通工程的读取路径
InputStream inputStream = new FileInputStream(new File("src/dbcp.properties"));
第三种获取方式【通用,防空格中文编码不读取】
String file = DBCP.class.getClassLoader().getResource("dbcp.properties").getFile();
String decode = URLDecoder.decode(file, "utf-8");
//System.out.println(file);
//System.out.println(decode);
InputStream inputStream = new FileInputStream(decode);
测试结果

JdbcDbcpUtil 工具类封装
public class JdbcDbcpUtil {
private static DataSource dataSource = null;
static {
String path = JdbcDbcpUtil.class.getClassLoader().getResource("dbcp.properties").getFile();
System.out.println(path);
try {
String decode = URLDecoder.decode(path, "utf-8");
InputStream inputStream = new FileInputStream(decode);
Properties properties = new Properties();
properties.load(inputStream);
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return null;
}
}
测试结果

Druid连接池实现
Maven依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
硬编码连接
@Test
public void dt1() throws SQLException {
DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///jdbc_db?serverTimezone=Asia/Shanghai");
dataSource.setUsername("root");
dataSource.setPassword("123456"); DruidPooledConnection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}

读取配置文件,读取方式跟DBCP几乎一样,配置文件都不需要改
@Test
public void dt2() throws Exception {
String path = DruidTest.class.getClassLoader().getResource("dbcp.properties").getFile();
String decode = URLDecoder.decode(path, "utf-8");
FileInputStream inputStream = new FileInputStream(decode);
Properties properties = new Properties();
properties.load(inputStream);
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}

JdbcDruidUtils 工具类封装
【对着DBCP的直接改连接池工厂就完事了】
public class JdbcDruidUtil {
private static DataSource dataSource = null;
static {
String path = JdbcDbcpUtil.class.getClassLoader().getResource("druid.properties").getFile();
System.out.println(path);
try {
String decode = URLDecoder.decode(path, "utf-8");
InputStream inputStream = new FileInputStream(decode);
Properties properties = new Properties();
properties.load(inputStream);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return null;
}
}
【Java】JDBC Part5 DataSource 连接池操作的更多相关文章
- Java java jdbc thin远程连接并操作Oracle数据库
JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...
- eclipse下jdbc数据源与连接池的配置及功能简介
今天在做四则运算网页版的时候遇到了一个困惑,由于需要把每个产生的式子存进 数据库,所以就需要很多次重复的加载驱动,建立连接等操作,这样一方面写程序不方便,加大了程序量,另一方面,还有导致数据库的性能急 ...
- CP30 ---DataSource连接池的创建过程
1.参看CP30文档quickStart 如下具体创建步骤 public DataSource getDs() throws Exception { //创建连接池对象 ComboPooledData ...
- Java自己动手写连接池四
Java自己动手写连接池四 测试: package com.kama.cn; import java.sql.Connection; public class Test { public static ...
- Java自己动手写连接池三
Java自己动手写连接池三,核心代码; package com.kama.cn; import java.sql.Connection;import java.util.ArrayList;impor ...
- Java 使用 DBCP mysql 连接池 做数据库操作
需要的jar包有 commons-dbutils , commons-dbcp , commons-pool , mysql-connector-java 本地database.propertties ...
- jdbc事务处理和连接池
JDBC: * JDBC概念:Java DataBase Connectivity(Java数据库连接) SUN公司提供的一组连接数据库API. * JDBC开发步骤: * 1.注册驱动. * 2.获 ...
- jdbc基础 (五) 连接池与数据源 DBCP以及C3P0的使用
一.连接池的概念和使用 在实际应用开发中,特别是在WEB应用系统中,如果JSP.Servlet或EJB使用JDBC直接访问数据库中的数据,每一次数据访问请求都必须经历建立数据库连接.打开数据库.存取数 ...
- JAVA中事物以及连接池
一.事物 什么是事物? 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元.这些单元要么全都成功,要么全都不成功. 做一件事情,这个一件事情中有多个 ...
- Spring整合JDBC和Druid连接池
我的博客名为黑客之谜,喜欢我的,或者喜欢未来的大神,点一波关注吧!顺便说一下,双十二快到了,祝大家双十二快乐,尽情的买买买~ 如果转载我的文章请标明出处和著名,谢谢配合. 我的博客地址为: https ...
随机推荐
- Nacos 版本不一致报错: Request nacos server failed
在做微服务开发中,测试环境使用Nacos没有问题,但是生产环境服务启动一直报错: com.alibaba.nacos.api.exception.NacosException: Request nac ...
- C# 利用Autofac批量接口注入依赖【学习记录】
背景: 本人在一位大佬的Colder框架中看到了这个接口注入,然后呢就想学习一下ioc思想与di设计模式.此写法给我的感觉就是 非常的 优雅 ,优雅永不过时.关于接口注入的概念和ioc和di具体是什么 ...
- react自定义导航组件 路由参数
为何需要自定义导航? 因为在项目中往往不是所有的声明式导航都是需要a标签完成,有时候可能需要别的标签,此时如果在需要的地方去写编程式导航就会有代码重复可能性,就在对于公共代码进行提取. 思路: 定义一 ...
- runliuv MSDN I TELL YOU
runliuv MSDN I TELL YOU 老站点:WIN SEVER ,VISUAL STUDIO 早期版本 老站点:https://msdn.itellyou.cn/ 新站点:最近的WIN10 ...
- Scrapy框架(五)--请求传参
在某些情况下,我们爬取的数据不在同一个页面中,例如,我们爬取一个电影网站,电影的名称,评分在一级页面,而要爬取的其他电影详情在其二级子页面中. 这时我们就需要用到请求传参. 请求传参的使用场景 当我们 ...
- 增补博客 第七篇 python 比较不同Python图形处理库或图像处理库的异同点
OpenCV.Pillow 和 scikit image OpenCV(OpenCV 是一个强大的计算机视觉库,它提供了各种图像处理和计算机视觉算法的实现,可以处理各种图像和视频数据. 异同点 跨平台 ...
- mybatis Selective动态判断属性值新增或修改操作,batch批量操作
mybatis Selective动态判断属性值新增或修改操作,batch批量操作 mybatis insert foreach批量添加https://www.cnblogs.com/oktokeep ...
- CloseableHttpClient设置超时时间demo 未设置默认是2分钟
# CloseableHttpClient设置超时时间demo 未设置默认是2分钟 import org.apache.http.HttpHeaders; import org.apache.http ...
- 【动手学深度学习】第五章笔记:层与块、参数管理、自定义层、读写文件、GPU
为了更好的阅读体验,请点击这里 由于本章内容比较少且以后很显然会经常回来翻,因此会写得比较详细. 5.1 层和块 事实证明,研究讨论"比单个层大"但"比整个模型小&quo ...
- 数据特征采样在 MySQL 同步一致性校验中的实践
作者:vivo 互联网存储研发团队 - Shang Yongxing 本文介绍了当前DTS应用中,MySQL数据同步使用到的数据一致性校验工具,并对它的实现思路进行分享. 一.背景 在 MySQL 的 ...