C3P0 实例化并配置 ComboPooledDataSource

也许创建c3p0池数据源的最直接方式就是实例化一个com.mchange.v2.c3p0.ComboPooledDataSource 。

这是一个JavaBean风格的,拥有一个无参数的public 构造方法的类,但当你使用它之前,你应该确保你设定了 jdbcUrl 属性。

你很可能也需要设定 user 和 password 属性。

如果你还没有在外部预加载老式的JDBC 驱动的话,你还应该设定 driverClass 属性。[code]ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( “org.postgresql.Driver” ); //loads the jdbc driver
cpds.setJdbcUrl( “jdbc:postgresql://localhost/testdb” );
cpds.setUser(“swaldman”);
cpds.setPassword(“test-password”);

// the settings below are optional – 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[/code]任何一个c3p0 DataSource实例的最初状态都取决于你提供的配置,或者仅仅是硬编码的默认配置[查看属性配置]。

c3p0-0.9.1之后支持了混合的,命名的配置(multiple, named configurations)。

如果你想使用命名配置,可以用一个命名配置名称作为参数来构造ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");com.mchange.v2.c3p0.ComboPooledDataSource:

当然,你仍然可以像上面提到的那样,用编程的方法来覆盖任何配置信息。

REF:
http://cwiki.ossez.com/pages/viewpage.action?pageId=4719787