c3p0连接池快速入门
为什么要使用连接池,这些基本也不用说那么多
以下为快速入门案例
包目录结构

配置文件c3p0-config.xml
<c3p0-config>
<!-- 默认配置,如果没有指定自己的,则使用这个配置 -->
<default-config>
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
<user-overrides user="test-user">
<property name="maxPoolSize">10</property>
<property name="minPoolSize">1</property>
<property name="maxStatements">0</property>
</user-overrides>
</default-config>
<!-- 命名的配置 -->
<named-config name="mytest">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/kafka2hive?useUnicode=true&characterEncoding=UTF-8</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">20</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">40</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
JdbcC3p0Util
public class JdbcC3p0Util {
private static ComboPooledDataSource dataSource;
static{
try {
dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/kafka2hive");
dataSource.setUser("root");
dataSource.setPassword("123456");
// * 最大连接数
dataSource.setMaxPoolSize(50);
// * 最小连接数
dataSource.setMinPoolSize(10);
// * 每次增长的个数
dataSource.setAcquireIncrement(5);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
public static void main(String[] args) throws Exception {
System.out.println(getConnection());
}
}
JdbcC3p0Util2
public class JdbcC3p0Util2 {
private static ComboPooledDataSource dataSource;
static{
dataSource = new ComboPooledDataSource("mytest"); //c3p0-config.xml <named-config name="mytest"> 配置名称
}
public static Connection getConnection() throws SQLException{
return dataSource.getConnection();
}
public static void main(String[] args) throws Exception {
System.out.println(getConnection());
}
}
根据自己业务场景使用,推荐使用第二种!!
c3p0连接池快速入门的更多相关文章
- C3P0连接池参数配置说明
C3P0连接池参数配置说明 created by cjk on 2017.8.15 常用配置 initialPoolSize:连接池初始化时创建的连接数,default : 3(建议使用) minPo ...
- c3p0连接池]
<c3p0-config> <!-- 默认配置 --> <default-config> <property name="jdbcUrl" ...
- c3p0连接池获得的Connection执行close方法后是否真的销毁Connection对象?
问题描述: jfinal做的api系统中,在正常调用接口一段时间后,突然再调用接口的时候,该请求无响应api系统后台也无错误信息 (就是刚开始接口调用是正常的,突然就无响应了) 于是啊,就开始找错误. ...
- C3P0连接池在hibernate和spring中的配置
首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...
- C3P0连接池问题,APPARENT DEADLOCK!!! Creating emergency..... [问题点数:20分,结帖人lovekong]
采用c3p0连接池,每次调试程序,第一次访问时(Tomcat服务器重启后再访问)都会出现以下错误,然后连接库需要很长时间,最终是可以连上的,之后再访问就没问题了,请高手们会诊一下,希望能帮小弟解决此问 ...
- HQL查询及Hibernate对c3p0连接池的支持
//HQL查询 // auto-import要设置true,如果是false,写HQL时要指定类的全名 //查询全部列 Query query = session.createQuery(" ...
- C3P0连接池详解及配置
C3P0连接池详解及配置 本人使用的C3P0的jar包是:c3p0-0.9.1.jar <bean id = "dataSource" class = "com.m ...
- 使用c3p0连接池
首先我们需要知道为什么要使用连接池:因为jdbc没有保持连接的能力,一旦超过一定时间没有使用(大约几百毫秒),连接就会被自动释放掉,每次新建连接都需要140毫秒左右的时间而C3P0连接池会池化连接,随 ...
- C3P0连接池详细配置
C3P0连接池详细配置 转自http://msq.javaeye.com/blog/60387 <c3p0-config> <default-config> <!--当连 ...
随机推荐
- webpack Entrypoint undefined = index.html
报错: module.exports增加配置stats: { children: false }即可解决:
- DevOps书单:调研了101名专家,推荐这39本必读书籍
任何一个领域都遵循从新人到熟手,从熟手到专家的路径.在成长过程中,DevOps人经常会陷入没人带,没人管,找不到职业方向的迷茫. DevOps是在商业演进与企业协作的进化过程中诞生的一个全新职业,被很 ...
- Mybatis(三)MyBatis 动态SQL
在 MyBatis 3 之前的版本中,使用动态 SQL 需要学习和了解非常多的标签,现在 MyBatis 采用了功能强大的 OGNL( Object-Graph Navigation Language ...
- expect自动远程拷贝脚本
expect自动远程拷贝脚本,利用rsync命令,脚本内容如下: #!/usr/bin/expect -- proc Usage_Exit {self} { puts "" put ...
- C++ GUI Qt4编程-创建自定义窗口部件
C++ GUI Qt4编程-创建自定义窗口部件 Qtqt4 通过Qt窗口部件进行子类化或者直接对QWidget进行子类化,就可以创建自定义窗口部件,下面示范两种方式,并且也会说明如何把自定义窗口部 ...
- python数据探索与数据与清洗概述
数据探索的核心: 1.数据质量分析(跟数据清洗密切联系,缺失值.异常值等) 2.数据特征分析(分布.对比.周期性.相关性.常见统计量等) 数据清洗的步骤: 1.缺失值处理(通过describe与len ...
- B/S上传文件夹
文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...
- 9-10 November
cout 和 printf 在 C++ 中的实现:四舍六入五随缘.比如 printf("%.0lf\n", x=1.5) => 1. 标准做法:printf("%d ...
- vue +ts 的一次踩坑日记
在vue的方法里面写事件的时候比如写一个路由跳转,方法大概如下: goBack1() { console.log(this); this.$router. ...
- fedora禁用(开机启动)服务和进程管理
首先要查看有哪些(开机启动)服务 chkconfig --list 或者: systemctl list-units 然后, 根据需要进行禁用服务的开机启动: chkconfig service_na ...