C3p0創(chuàng)建方式
c3p0有3種創(chuàng)建方式(1)直接實(shí)例化并配置comboPooledDataSource
bean航罗。(2)使用DataSources工廠類
。(3)實(shí)現(xiàn)PoolBackedDataSource
類(abstract類)涂炎。即實(shí)現(xiàn)自己的DataSource阻星。
一、實(shí)例化并配置ComboPooledDataSource
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
If you obtain a DataSource by instantiating a ComboPooledDataSource
, configure it by simply calling appropriate setter methods offered by that class before attempting a call to getConnection()
.
常用的最直接的方式是實(shí)例化ComboPooledDataSource中剩。這是一個(gè)JavaBean風(fēng)格的類邪驮,提供一個(gè)public莫辨,無參的構(gòu)造器。配置參數(shù)至少要求配置jdbcUrl毅访。
c3p0支持以name命名的數(shù)據(jù)源沮榜。ComboPooledDataSource提供一個(gè)String的構(gòu)造器,傳入name即可俺抽。
ComboPooledDataSource cpds = new ComboPooledDataSource("intergalactoApp");
二敞映、使用DataSources facotry工廠類
另一種方法是通過DataSources工廠類創(chuàng)建unpooled DataSource數(shù)據(jù)源,并且可通過unpooled DataSource創(chuàng)建pooled DataSource。
DataSource ds_unpooled = DataSources.unpooledDataSource("jdbc:postgresql://localhost/testdb",
"swaldman",
"test-password");
DataSource ds_pooled = DataSources.pooledDataSource( ds_unpooled );
自定義配置項(xiàng)
指定一個(gè)Map存放配置項(xiàng)磷斧,使用pooledDataSource重載方法振愿。
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
ds_pooled = DataSources.pooledDataSource( ds_unpooled, overrides );
為數(shù)據(jù)源指定名字
pooledDataSource重在方法提供命名name
ds_pooled = DataSources.pooledDataSource( ds_unpooled, "intergalactoApp", overrides );