设为首页 加入收藏

TOP

[c3p0]第二篇:使用c3p0(二)
2015-11-21 01:45:24 来源: 作者: 【 】 浏览:2
Tags:c3p0 第二篇 使用
ooledDataSource using the a named configuration and specified overrides "intergalactoApp" is a named configuration ds_pooled = DataSources.pooledDataSource(ds_unpooled, "intergalactoApp", overrides);

?

查询PooledDataSource的当前状态

c3p0 DataSource 支持池,包含ComboPooledDataSource实现并通过DataSources.pooledDataSource(...)返回对象,所有com.mchange.v2.c3p0.PooledDataSource的接口实现,都可以利用多种方法查询DataSource连接池的状态。下面是查询DataSource的简单代码:

?

// Fetch a JNDI-bound DataSource
InitialContext ictx = new InitialContext();
DataSource ds = (DataSource) ictx.lookup("java:comp/env/jdbc/myDataSource");

// make sure it's a c3p0 PooledDataSource
if (ds instanceof PooledDataSource) {
	PooledDataSource pds = (PooledDataSource) ds;
	System.err.println("num_connections: " + pds.getNumConnectionsDefaultUser());
	System.err.println("num_busy_connections: " + pds.getNumBusyConnectionsDefaultUser());
	System.err.println("num_idle_connections: " + pds.getNumIdleConnectionsDefaultUser());
	System.err.println();
} else {
	System.err.println("Not a c3p0 PooledDataSource!");
}

?

状态查询方法都类似于以下三个重载形式:

?

public int getNumConnectionsDefaultUser();
public int getNumCOnnections(String username, String password);
public int getNumConnectionsAllUsers();

?

c3p0以不同的验证信息维护各自的连接池。有众多的方法让你查询单独某个池的状态,或者是汇总所有的数据。值得注意的是像maxPoolSize这样的池配置参数是在每个认证上都会使用的。例如,如果你已经设置了maxPoolSize=20,DataSource正在管理着两对username-password,默认的一个,另一个通过调用getConnection(user,password)建立,调用getNumConnectionsAllUsers(),最大能看到的连接数应该是40.

?

多数应用只需要从DataSource获取默认的认证连接,典型的通过getXXXDefaultUser()收集连接数据。

?

还有连接池的真实数据,你可以取到每个DataSource的线程池状态数据。请参考PooledDataSource可选操作的完整列表。

?

使用C3P0Registry引用DataSource

如果通过JNDI或者是其它方式不方便或是不太可能得到DataSource的引用,你可以使用C3P0Registry类找到有效的c3p0 DataSource,它包含三个静态方法:

?

public static Set getPooledDataSources();
public static Set pooledDataSourcesByName(String dataSourceName);
public static PooledDataSource pooledDataSourceByName(String dataSourceName);

?

第一个方法返回给你c3p0 PooledDataSources的Set集合。如果你确信你的应用只会产生一个PooledDataSources,或者你能通过配置参数区分DataSource(通过"getters"检查),第一种方法就足够了。

?

因为并不总是如此,c3p0 PooledDataSources有一个叫做dataSourceName的特殊属性,当你构造DataSource的时候,你可以直接设置dataSourceName属性,或者像其它属性一样对其进行配置。否则,dataSourceName将会默认两种情况:

?

1、如果你用一个命名配置构造DataSource,就是你的配置名称。

2、如果你使用默认的配置,就会有一个唯一的名字,但不可预测。


这里并没有保证dataSourceName是唯一的。例如,如果两个c3p0 DataSource共享一个命名配置,而且你没有编程式的设置dataSourceName,那么这两个数据源将会共享配置名称。可以使用pooledDataSourcesByName(...)得到特定dataSourceName的所有数据源。

?

如果你确定DataSource的名字是唯一的(如果你想要使用C3P0Registry找到DataSource,你通常会愿意这么做),你可以使用更方便的方法pooledDataSourceByName(...),它将直接返回指定名字的DataSource的引用或者是null(如果没有DataSource)。如果你在多个共享名称的DataSource中使用pooledDataSourceByName(...),将会返回哪一个DataSource是没有明确定义的。

?

c3p0 PooledDataSources使用之后的清理

c3p0创建DataSources之后,最简单的清理方法是使用DataSources类定义的静态销毁方法。仅有PooledDataSources需要清理,如果在一个不带连接池的DataSource或者是non-c3p0 DataSource中使用,DataSources.destroy(...)也是没有危害的。

?

DataSource ds_pooled = null;
try {
	DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb", "swaldman", "test-password");
	ds_pooled = DataSources.pooledDataSource(ds_unpooled);
	// Do all kinds of stuff with that sweet pooled DataSo
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇11gRAC打最新的11.2.0.4.7补丁记录 下一篇全局分区索引和本地索引示例

评论

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