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> <!--当连 ...
随机推荐
- css3 transform中的matrix矩阵
CSS3中的矩阵CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform),后者则是3D变换.2D变换矩阵为3*3, 如上面矩阵示 ...
- 安装了vs2019 编译node-sass node-gyp 找不到编译器的解决方法
1 新建powershell脚本文件 <# This is a workaround for "node-gyp is unable to find msbuild if VS2019 ...
- 【串线篇】spring boot嵌入式Servlet容器自动配置原理
EmbeddedServletContainerAutoConfiguration:嵌入式的Servlet容器自动配置? @AutoConfigureOrder(Ordered.HIGHEST_PREC ...
- spring security基本知识(四) WebSecurity
1.创建一个Filter 现在web.xml文档中声明一个filter class="org".springframework.web.filter.DelegatingFil ...
- P4716 【模板】最小树形图
题意 说一下我对朱刘算法的理解: 首先我们考虑树形图的性质:每个点除了根节以外都含有一条入边. 因此我们可以有一个贪心的想法:对每个点(除了根节点)找到一条最短的入边,但是这样会出现环,如下图: 我们 ...
- git的初步研究1
工作中很多项目再往git上迁移,所以打算研究下git git是个版本控制系统 理解git工作区.暂存区.版本库的概念 工作区:在电脑中能看到的目录 暂存区:index即索引 即首先add加入暂存区 c ...
- linux运维、架构之路-linux目录结构
1.linux重要目录 重要目录 说明 /etc 存放系统配置文件.服务启动命令的目录 /root 超级管理员的家目录 /sbin和usr/sbin 超级用户命令的目录 /boot 系统引导程序所在的 ...
- SQL Server性能调优--优化建议(一)
序言 当数据量小的时候,SQL优化或许无关紧要,但是当数据量达到一定量级之后,性能优化将变得至关重要,甚至决定系统成败. 定位慢查询 查询编译以来cpu耗时总量最多的前50条 --查询编译以来 cpu ...
- 三维显示插件——C++
Qt 3D 构建自己的C/C++插件开发框架——系列:https://blog.csdn.net/chgaowei/article/details/4545211 如何使用Qt 3D开发3D场景程序: ...
- sed的一些应用
1. sed 使用变量进行替换,注意使用参数 r 时,需要放在参数 i 的前面 下面这个例子是用2.txt中的版本号替换docker-compose.yml中的版本号,其中变量UPGRADE_NAME ...