设为首页 加入收藏

TOP

[c3p0]第二篇:使用c3p0(一)
2015-11-21 01:45:24 来源: 作者: 【 】 浏览:3
Tags:c3p0 第二篇 使用

前言

译文是根据c3p0-0.9.5.1版本的官方文档,加上自己的理解,整理翻译而成。能力有限,译文内容如果有误或是理解有偏差,还请大家纠正!

?

使用c3p0

从用户的角度看,c3p0只是简单的为用户提供符合jdbc标准的DataSource对象。当创建这些DataSource对象的时候,用户可以控制与其相关的各种属性。一旦DataSource创建完成,DataSource背后的东西对用户来讲,就是完全透明的。

?

三种方式获取c3p0带连接池的DataSource

1、直接实例化并配置一个CombopooledDataSource bean;

2、使用DataSource工厂类;

3、通过直接实例化PoolBackedDataSource并设置它的ConnectionPoolDataSource来创建的带有连接池的DataSource。

?

大部分用户会发现实例化CombopooledDataSource一般来讲是最方便的途径。一旦实例化,c3p0的DataSource几乎就可以绑定到任何JNDI兼容的命名服务。

?

用户创建DataSource的时候,如果没有特别指定其各种属性,c3p0会提供默认的属性值,是通过硬编码的方式实现的。但是,用户也是可以通过配置文件等方式重写这些属性值的,如果是以配置文件的方式重写,那么配置文件需要放在CLASSPATH下或是ClassLoader可以找到的地方。

?

c3p0数据源可以通过多种方式配置

1、命名为c3p0.properties的java.util.Properties属性文件。

2、更为先进的HOCON配置文件(例如:application.conf,application.json)。
3、命名为c3p0-config.xml的XML文件。

?

实例化并配置ComboPooledDataSource

最简单最直接的创建c3p0带有连接池的DataSource就是实例化com.mchange.v2.c3p0.ComboPooledDataSource.这是一个JavaBean风格的类,有一个public的无惨构造器,但是在使用DataSource之前,你必须确保至少设置一个jdbcUrl属性。你还可以设置user和password,如果你使用旧的不能预加载的JDBC驱动,你还应该设置driverClass。

?

ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("org.postgresql.Driver");// loads the driver
cpds.setJdbcUrl("jdbc:postgresql://localhost/testdb");
cpds.setUser("swaldman");
cpds.setPassword("test-password");

// The settings below are potional -- c3p0 can work with defaults
cpds.setMinPoolSize(5);
cpds.setAcquireIncrement(5);
cpds.setMaxPoolSize(20);

// The DataSource cpds is now a fully configured and usable pooled DataSource


?

c3p0数据源的属性值可以由你来配置,或者直接使用默认的。c3p0支持命名配置,所以你可以同时配置多个DataSource。如果你想使用命名配置,那么就要使用“配置名称”作为构造参数来构造com.mchange.v2.c3p0.ComboPooledDataSource

?

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


?

当然,你也可以重写它的任何配置属性,和上面一样。

?

使用数据源工厂类

作为可供选择的一种方案,你可以使用静态工厂类com.mchange.v2.c3p0.DataSource以传统的JDBC驱动的方式去创建不带连接池的DataSource,然后从不带连接池的数据源创建带连接池的数据源。

?

DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
DataSource ds_pooled = DataSources.pooledDataSource(ds_unpooled);

// The DataSource ds_pooled is now a fully configured and usable pooled DataSource.
// The DataSource is using a default pool configuration and Postgres' JDBC driver
// is presumed to have already been loaded via the jdbc.drivers system property or an
// explicit call to Class.forName("org.postgresql.Driver") elsewhere.
...

?

如果你使用DataSources工厂类,又想以编程的方式重写默认配置参数,你可以提供一个map集合属性

?

DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
Map overrides = new HashMap();
overrides.put("maxStatements", "200");// Stringified property values work
overrides.put("maxPoolSize", new Integer(50));// "boxed primitives" also work

// create the PooledDataSource using the default configuration and our overrides
DataSource ds_pooled = DataSources.pooledDataSource(ds_unpooled, overrides);

// The DataSource ds_pooled is now a fully configured and usable pooled DataSource,
// with Statement caching enabled for a maximum of up to 200 statements and a maximum of 50 Connection.
...

?

如果你使用命名配置,你可以直接指定数据源的默认配置

?

// Create the P
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇11gRAC打最新的11.2.0.4.7补丁记录 下一篇全局分区索引和本地索引示例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: