You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Vinicius Caldeira Carvalho <vi...@squadra.com.br> on 2005/02/22 14:14:51 UTC

DBCP usage question

Hi there I've a method to create a poolabe datasource. I read the 
configuration from a properties file. This file has this layout

DATASOURCE_NAME.driver
DATASOURCE_NAME.url

so I can have more than one ds per file. My method looks like this:

private DataSource setupDataSource(String dataSource) throws Exception {
InputStream in = loader.getResourceAsStream("dbcp.properties");
Properties props = new Properties();
props.load(in);

GenericObjectPool connectionPool = new GenericObjectPool(null);
connectionPool.setMaxActive(Integer.decode(props.getProperty("maxActive")).intValue());
connectionPool.setMaxIdle(Integer.decode(props.getProperty("maxIdle")).intValue());

BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName(props.getProperty(dataSource + ".driver"));
bds.setUrl(props.getProperty(dataSource + ".url"));
bds.setUsername(props.getProperty(dataSource + ".user"));
bds.setPassword(props.getProperty(dataSource + ".password"));

ConnectionFactory connectionFactory = new DataSourceConnectionFactory(bds);

PoolableConnectionFactory poolableConnectionFactory = new
PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);

PoolingDataSource pds = new PoolingDataSource(connectionPool);

return pds;

}
I just 'd like to know if this is ok, or am I missing something?

thanks all


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: DBCP usage question

Posted by Dirk Verbeeck <di...@pandora.be>.
The BasicDataSource already does connection pooling so you can simply do:
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName(props.getProperty(dataSource + ".driver"));
bds.setUrl(props.getProperty(dataSource + ".url"));
bds.setUsername(props.getProperty(dataSource + ".user"));
bds.setPassword(props.getProperty(dataSource + ".password"));
return bds;

Also take a look at BasicDataSourceFactory.createDataSource(properties)

It doesn't handle the prefix but otherwise it creates and sets all 
known properties on a BasicDataSource.

Regards
Dirk


Vinicius Caldeira Carvalho wrote:
> Hi there I've a method to create a poolabe datasource. I read the 
> configuration from a properties file. This file has this layout
> 
> DATASOURCE_NAME.driver
> DATASOURCE_NAME.url
> 
> so I can have more than one ds per file. My method looks like this:
> 
> private DataSource setupDataSource(String dataSource) throws Exception {
> InputStream in = loader.getResourceAsStream("dbcp.properties");
> Properties props = new Properties();
> props.load(in);
> 
> GenericObjectPool connectionPool = new GenericObjectPool(null);
> connectionPool.setMaxActive(Integer.decode(props.getProperty("maxActive")).intValue()); 
> 
> connectionPool.setMaxIdle(Integer.decode(props.getProperty("maxIdle")).intValue()); 
> 
> 
> BasicDataSource bds = new BasicDataSource();
> bds.setDriverClassName(props.getProperty(dataSource + ".driver"));
> bds.setUrl(props.getProperty(dataSource + ".url"));
> bds.setUsername(props.getProperty(dataSource + ".user"));
> bds.setPassword(props.getProperty(dataSource + ".password"));
> 
> ConnectionFactory connectionFactory = new DataSourceConnectionFactory(bds);
> 
> PoolableConnectionFactory poolableConnectionFactory = new
> PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true); 
> 
> 
> PoolingDataSource pds = new PoolingDataSource(connectionPool);
> 
> return pds;
> 
> }
> I just 'd like to know if this is ok, or am I missing something?
> 
> thanks all


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org