title: Connection Pooling with Apache DBCP
body: <p>I want to use <a href="http://commons.apache.org/dbcp/" rel="noreferrer">Apache Commons DBCP</a> to enable connection pooling in a Java Application (no container-provided DataSource in this). In many sites of the web -including <a href="http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/BasicDataSourceExample.java?view=markup" rel="noreferrer">Apache site</a>- the usage of the library is based in this snippet:</p>
  
  <pre><code>BasicDataSource ds = new BasicDataSource();
  ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
  ds.setUsername("scott");
  ds.setPassword("tiger");
  ds.setUrl(connectURI);  
  </code></pre>
  
  <p>Then you get your DB connections through the getConnection() method. But on other sites -and <a href="http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/doc/PoolingDataSourceExample.java?view=markup" rel="noreferrer">Apache Site also</a>- the Datasource instance is made through this:</p>
  
  <pre><code>ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
  PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory);
  ObjectPool objectPool = new GenericObjectPool(poolableConnectionFactory);
  PoolingDataSource dataSource = new PoolingDataSource(objectPool);
  </code></pre>
  
  <p>What's the difference between them? I'm using connection pooling with <code>BasicDataSource</code>, or I need an instance of <code>PoolingDataSource</code> to work with connection pooling? Is <code>BasicDataSource</code> thread-safe (can I use it as a Class attribute) or I need to synchronize its access?</p>