先看官网给的范例:

import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources; /**
* This example shows how to acquire a c3p0 DataSource and
* bind it to a JNDI name service.
*/
public final class JndiBindDataSource
{
// be sure to load your database driver class, either via
// Class.forName() [as shown below] or externally (e.g. by
// using -Djdbc.drivers when starting your JVM).
static
{
try
{ Class.forName( "com.mysql.jdbc.Driver" ); }
catch (Exception e)
{ e.printStackTrace(); }
} public static void main(String[] argv)
{
try
{
// let a command line arg specify the name we will
// bind our DataSource to.
String jndiName = argv[0]; // acquire the DataSource using default pool params...
// this is the only c3p0 specific code here
DataSource unpooled = DataSources.unpooledDataSource("jdbc:mysql://127.0.0.1:3306/gpsdata",
"root",
"root");
DataSource pooled = DataSources.pooledDataSource( unpooled ); // Create an InitialContext, and bind the DataSource to it in
// the usual way.
//
// We are using the no-arg version of InitialContext's constructor,
// therefore, the jndi environment must be first set via a jndi.properties
// file, System properties, or by some other means.
InitialContext ctx = new InitialContext();
ctx.rebind( jndiName, pooled );
System.out.println("DataSource bound to nameservice under the name \"" +
jndiName + '\"');
}
catch (Exception e)
{ e.printStackTrace(); }
} static void attemptClose(ResultSet o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} static void attemptClose(Statement o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} static void attemptClose(Connection o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
} private JndiBindDataSource()
{}
}

1.建立 com.mchange.v2.c3p0.ComboPooledDataSource

这是一个JavaBean,在使用前应设置它的jdbcURL、user、password和driverClass。其他参数参考configuration properties

[java] view
plain
copy

  1. ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "org.postgresql.Driver" );
  2. cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" ); cpds.setUser("swaldman");
  3. cpds.setPassword("test-password");
  4. // 下面的设置是可选的,c3p0可以在默认条件下工作,也可以设置其他条件
  5. cpds.setMinPoolSize(5);
  6. cpds.setAcquireIncrement(5);
  7. cpds.setMaxPoolSize(20);

也可以使用命名Configuration

[java] view
plain
copy

  1. ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");

2.使用工厂类com.mchange.v2.c3p0.DataSources

com.mchange.v2.c3p0.DataSources 可以按照传统的JDBC驱动建立一个无连接池的DataSource,然后转化为连接池的DataSource。

[java] view
plain
copy

  1. DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
  2. DataSource ds_pooled = DataSources.pooledDataSource( ds_unpooled );
  3. // 此时DataSource已经可以使用,但应该显示的设置driver Class
  4. Class.forName("org.postgresql.Driver");

如果想设置其中的参数,可以将参数放入一个Map中

[java] view
plain
copy

  1. DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
  2. Map overrides = new HashMap();
  3. overrides.put("maxStatements", "200"); overrides.put("maxPoolSize", new Integer(50));
  4. //建立一个包括默认值和设置值的PooledDataSource
  5. ds_pooled = DataSources.pooledDataSource( ds_unpooled, overrides );

如果使用命名的Configuration,可以如下

[java] view
plain
copy

  1. ds_pooled = DataSources.pooledDataSource( ds_unpooled, "intergalactoAppConfig", overrides );

销毁DataSource有两种方式

DataSource.destroy()方式

[java] view
plain
copy

  1. DataSource ds_pooled = null;
  2. try {
  3. DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
  4. ds_pooled = DataSources.pooledDataSource( ds_unpooled );
  5. // 下面正常使用DataSource...
  6. }
  7. finally {
  8. DataSources.destroy( ds_pooled );
  9. }

另一种是PooledDataSource 接口提供的close() 方法

[java] view
plain
copy

  1. static void cleanup(DataSource ds) throws SQLException {
  2. // 确定是否为c3p0的PooledDataSource
  3. if ( ds instanceof PooledDataSource) {
  4. PooledDataSource pds = (PooledDataSource) ds;
  5. pds.close();
  6. } else
  7. System.err.println("Not a c3p0 PooledDataSource!");
  8. }

Java中使用C3P0连接池的更多相关文章

  1. Spring框架中 配置c3p0连接池 完成对数据库的访问

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  2. JAVA中事物以及连接池

    一.事物 什么是事物? 事务,一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元.这些单元要么全都成功,要么全都不成功. 做一件事情,这个一件事情中有多个 ...

  3. java学习笔记—c3p0连接池与元数据分析(42)

    第一步:导入c3p0包 第二步:在classpath目录下,创建一个c3p0-config.xml <?xml version="1.0" encoding="UT ...

  4. Spring框架中 配置c3p0连接池

    开发准备: 1.导入jar包: ioc基本jar jdbcTemplate基本jar c3p0基本jar 别忘了mysql数据库驱动jar 原始程序代码:不使用配置文件方式(IOC)生成访问数据库对象 ...

  5. Hibernate的配置中,c3p0连接池相关配置

    一.配置c3p0 1.导入 hibernate-c3po连接池包,Maven地址是:http://mvnrepository.com/artifact/org.hibernate/hibernate- ...

  6. C3P0连接池在hibernate和spring中的配置

    首先为什么要使用连接池及为什么要选择C3P0连接池,这里就不多说了,目前C3P0连接池还是比较方便.比较稳定的连接池,能与spring.hibernate等开源框架进行整合. 一.hibernate中 ...

  7. Spring之c3p0连接池配置和使用

    1.导入包:c3p0和mchange包 2.代码实现方式: package helloworld.pools; import com.mchange.v2.c3p0.ComboPooledDataSo ...

  8. C3P0连接池工具类实现步骤及方法

    C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包 ...

  9. (七)Spring 配置 c3p0 连接池

    目录 在 Spring 核心配置文件中配置 c3p0 连接池 配置 JdbcTemplate 对象 在 service 层注入 userDao 在 UserDao 里面注入 JdbcTemplate ...

随机推荐

  1. ROS_Kinetic_x 基於ROS和Gazebo的RoboCup中型組仿真系統(多機器人協作)

    國防科學技術大學發布了RoboCup中型組仿真平臺,基於ROS和Gazebo設計. 該平臺可以用於多機器人協作研究.參考資料如下: ROS新聞:1    http://www.ros.org/news ...

  2. 【Unity Shaders】ShadowGun系列之一——飞机坠毁的浓烟效果

    写在前面 最近一直在思考下面的学习该怎么进行,当然自己有在一边做项目一边学OpenGL,偶尔翻翻论文之类的.但是,写shader是一个需要实战和动手经验的过程,而模仿是前期学习的必经之路.很多人都会问 ...

  3. 文件操作(File类等)API摘要

    Console 此类包含多个方法,可访问与当前 Java 虚拟机关联的基于字符的控制台设备(如果有). 虚拟机是否具有控制台取决于底层平台,还取决于调用虚拟机的方式.如果虚拟机从一个交互式命令行开始启 ...

  4. Android开发技巧——PagerAdapter的再次简单封装

    这次再对内容为View的ViewPager的适配器PagerAdapter进行简单的封装,支持List数据和SparseArray的数据,带更新视图功能. 首先,先贴上最上面的抽象类代码: /* * ...

  5. Linux Debugging(六): 动态库注入、ltrace、strace、Valgrind

    实际上,Linux的调试方法非常多,针对不同的问题,不同的场景,不同的应用,都有不同的方法.很难去概括.本篇文章主要涉及本专栏还没有涵盖,但是的确有很重要的方法.本文主要包括动态库注入调试:使用ltr ...

  6. phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)

    1.  在使用phantomjs的时候需要下载phantomjs,网上有window版本和Linux版本.将phantomjs放在Linux上的指定位置之后(如下面的/home/tpl/phantom ...

  7. 1016. Phone Bills (25) -vector排序(sort函数)

    题目如下: A long-distance telephone company charges its customers by the following rules: Making a long- ...

  8. C++对象模型(四):class成员初始化列表(Member Initialization List)

    本文是Inside C++ Object Model Chapter 2 部分的读书笔记. 编译器如何处理初始化成员列表的. 下列情况中,必须要使用member initialization list ...

  9. 详解ebs接口之客户配置文件导入(一)

    DECLARE l_rec_profile_t hz_customer_profile_v2pub.customer_profile_rec_type; l_rec_profile hz_custom ...

  10. javascript的介绍,实现和输出以及语法-javascript学习之旅(1)

    javascript的介绍 : 1.javascript死互联网最流行的脚本语言,可用于web和html,并且可用于服务器,pc和移动端 2.javascript脚本语言: 1.是一种轻量级的脚本语言 ...