-
c3p0-config.xml
c3p0-config.xml
一般将 c3p0 连接池的配置文件放置到到src目录下,该文件制订了连接数据库和连接池的相关参数。可以更方便的连接MySQL数据库。
<?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/jdbc</property>
<property name="user">root</property>
<property name="password">java</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</default-config>
<!-- 数据源名称,代表连接池-->
<named-config name="mySource">
<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">xxx</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">5</property>
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
示例代码
无xml配置文件
public void testC3P0_01(){
//创建一个数据源对象
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//通过配置文件获取相关的信息
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\mysql.properties"));
//读取相关的属性值
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
//给数据源设置相关的参数
comboPooledDataSource.setDriverClass(driver);//驱动类
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(password);
//初始化连接数
comboPooledDataSource.setInitialPoolSize(10);
//设置最大连接数
comboPooledDataSource.setMaxPoolSize(50);
//测试连接池效率,连接MySQL5000次
long start = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
//得到连接
Connection connection = comboPooledDataSource.getConnection();//这个方法就是从 DataSource 接口实现
// System.out.println("连接成功!");
connection.close();//关闭连接
}
long end = System.currentTimeMillis();
System.out.println("用时:"+(end-start));//1800ms左右
} catch (Exception e) {
e.printStackTrace();
}
}
有xml配置文件
public void testC3P0_02() throws SQLException {
ComboPooledDataSource mySource = new ComboPooledDataSource("mySource");
//下面的代码用来计算连接 5000 次数据库所需的时间
long start = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
Connection connection = mySource.getConnection();
// System.out.println("2连接成功!");
connection.close();
}
long end = System.currentTimeMillis();
System.out.println("配置文件连接用时:"+(end-start));//91ms
}
从上面的代码可以发现,c3p0可以更高效地连接数据库,而且所提供的的配置文件,使得在开发过程中的代码大幅度减少,不愧为一大利器。
原文:https://www.cnblogs.com/nanfengashuai/p/15633904.html
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比