之前已经讲过dbcp可以用于数据库连接池进行管理。另一种技术c3p0也可以用于数据库连接池管理,其中Spring等框架都是基于c3p0技术进行数据库连接池管理的。

使用之前需要引入 c3p0-0.9.5.2.jar 和 mchange-commons-java-0.2.11.jar 包,主要的类是ComboPooledDataSource,也有两种方式进行设置。一种是代码中进行设置,一种是在配置文件中设置。主要区别就是这种方式只有一个主要类ComboPooledDataSource。

他们之间可能方法名有所不同,但是功能是一样的。

1)在代码中进行配置,代码如下

ComboPooledDataSource dataSource = new ComboPooledDataSource();

        try {
dataSource.setJdbcUrl("jdbc:mysql:///mydb?useSSL=true");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setUser("root");
dataSource.setPassword("");
dataSource.setMaxIdleTime();
dataSource.setMaxPoolSize();
dataSource.setInitialPoolSize();
Connection conn = dataSource.getConnection(); String sql = "select * from user where id=?";
QueryRunner qr = new QueryRunner();
User user = qr.query(conn, sql, new BeanHandler<User>(User.class), ); System.out.println(user); dataSource.close(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

2) 在配置文件中进行设置

注意:配置文件命名是 c3p0-config.xml。系统默认回去 CLASSPATH、WEB-INF/classes文件夹中搜索配置文件,所以把他放在 src 目录下即可。

ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql_config");   //使用指定配置名的配置
try {
Connection conn = dataSource.getConnection();
String sql = "select * from user where id=?";
QueryRunner qr = new QueryRunner();
User user = qr.query(conn, sql, new BeanHandler<User>(User.class), ); System.out.println(user); dataSource.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

配置文件中可以进行设置的参数如下:

acquireIncrement
acquireRetryAttempts
acquireRetryDelay
autoCommitOnClose
automaticTestTable
breakAfterAcquireFailure
checkoutTimeout
connectionCustomizerClassName
connectionTesterClassName
contextClassLoaderSource
dataSourceName
debugUnreturnedConnectionStackTraces
driverClass
extensions
factoryClassLocation
forceIgnoreUnresolvedTransactions
forceSynchronousCheckins
forceUseNamedDriverClass
idleConnectionTestPeriod
initialPoolSize
jdbcUrl
maxAdministrativeTaskTime
maxConnectionAge
maxIdleTime
maxIdleTimeExcessConnections
maxPoolSize
maxStatements
maxStatementsPerConnection
minPoolSize
numHelperThreads
overrideDefaultUser
overrideDefaultPassword
password
preferredTestQuery
privilegeSpawnedThreads
propertyCycle
statementCacheNumDeferredCloseThreads
testConnectionOnCheckin
testConnectionOnCheckout
unreturnedConnectionTimeout
user

配置文件样例如下

<c3p0-config>
<default-config>
<property name="checkoutTimeout"></property>
<property name="idleConnectionTestPeriod"></property>
<property name="initialPoolSize"></property>
<property name="maxIdleTime"></property>
<property name="maxPoolSize"></property>
<property name="minPoolSize"></property> <user-overrides user="test-user">
<property name="maxPoolSize"></property>
<property name="minPoolSize"></property>
<property name="maxStatements"></property>
</user-overrides> </default-config>
<named-config name="mysql_config">
<property name="initialPoolSize"></property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb?useSSL=true</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="maxPoolSize"></property>
<property name="initialPoolSize"></property>
<property name="maxIdleTime"></property>
<property name="password"></property>
<property name="user">root</property> </named-config> </c3p0-config>

c3p0数据库连接池管理的更多相关文章

  1. 【Java EE 学习 16 上】【dbcp数据库连接池】【c3p0数据库连接池】

    一.回顾之前使用的动态代理的方式实现的数据库连接池: 代码: package day16.utils; import java.io.IOException; import java.lang.ref ...

  2. C3P0数据库连接池使用方法

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  3. paip.c3p0 数据库连接池 NullPointerException 的解决...

    paip.c3p0 数据库连接池 NullPointerException 的解决... 程序ide里面运行正常..外面bat运行错误.. 作者Attilax  艾龙,  EMAIL:14665198 ...

  4. paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out

    paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out 作者Attilax ...

  5. [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. c3p0数据库连接池(作用不重复)

    /* * c3p0数据库连接池: * 只被初始化一次 * connection对象进行close时,不是正的关闭,而是将该数据连接归还给数据库连接池 * * */ 四个架包 mysql-connect ...

  7. springboot 使用c3p0数据库连接池

    springboot 使用c3p0数据库连接池的方法  本文转自:http://www.cnblogs.com/xiaosiyuan/p/6255292.html 使用springboot开发时,默认 ...

  8. c3p0 数据库连接池相关知识

    c3p0数据库连接池的配置文件放在eclipse的src目录下,代码就可以识别. c3p0的配置文件的内容如下: <!-- Uncomment and set any of the option ...

  9. c3p0数据库连接池无法连接数据库—错误使用了username关键字

    一.问题描述 上篇博客说到了关于maven无法下载依赖jar包的问题,这篇博客再说一下关于在本个项目中遇到的关于使用C3P0连接池连接数据库的问题,真心很奇葩,在此,也请大家引起注意.首先看我的项目基 ...

随机推荐

  1. 执行计划解读 简朝阳 (Sky Jian) and 那蓝蓝海

    http://greemranqq.iteye.com/blog/2072878 http://www.mysqlab.net/ http://www.mysqlpub.com/ http://blo ...

  2. 常见dotNet加密保护工具分析介绍(转)

    本文主要介绍一些dotNet加密保护工具的原理以及就其脱壳进行简单探讨.remotesoft protector.maxtocode..Net Reactor.Cliprotector  .themi ...

  3. LINUX开启允许对外访问的网络端口命令

    LINUX通过下面的命令可以开启允许对外访问的网络端口: /sbin/iptables -I INPUT -p tcp --dport 8000 -j ACCEPT #开启8000端口 /etc/rc ...

  4. Java垃圾回收精粹 — Part4

    Java垃圾回收精粹分4个部分,本篇是第4部分.在第4部分里介绍了G1收集器.其他并发收集器以及垃圾收集监控和调优. Garbage First (G1) 收集器 G1 (-XX:+UseG1GC)收 ...

  5. poj 3130 How I Mathematician Wonder What You Are! - 求多边形有没有核 - 模版

    /* poj 3130 How I Mathematician Wonder What You Are! - 求多边形有没有核 */ #include <stdio.h> #include ...

  6. Pydoc 本地 HTML 形式查看

    Pydoc 本地 HTML 形式查看 我们在编写Python代码时,常常会去查询某些模块及函数的使用,会选择 dir() 及 help() 函数.或查看 CHM 格式的Python帮助文档.或查看Py ...

  7. 常见C++内存池技术

    原文:http://www.cppblog.com/weiym/archive/2013/04/08/199238.html 总结下常见的C++内存池,以备以后查询.应该说没有一个内存池适合所有的情况 ...

  8. sqlalchemy: TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30

    mysql建立的连接要及时删除,不然连接池资源耗尽 相关文章参考: http://blog.csdn.net/robinson1988/article/details/4713294 http://b ...

  9. MD5在线查询的实现

    #!/usr/bin/env python # -*- coding: UTF-8 -*- ''' 快速查询md5值工具,通过模拟浏览器发包请求http://cmd5.com和 http://xmd5 ...

  10. hadoop中的序列化与Writable接口

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable-interface.html,转载请注明源地址. 简介 序列化和反序列化就是结构化对象 ...