Connection Pooling with Apache DBCP

烂漫一生 提交于 2019-12-29 14:15:10

问题


I want to use Apache Commons DBCP to enable connection pooling in a Java Application (no container-provided DataSource in this). In many sites of the web -including Apache site- the usage of the library is based in this snippet:

BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setUrl(connectURI);  

Then you get your DB connections through the getConnection() method. But on other sites -and Apache Site also- the Datasource instance is made through this:

ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory);
ObjectPool objectPool = new GenericObjectPool(poolableConnectionFactory);
PoolingDataSource dataSource = new PoolingDataSource(objectPool);

What's the difference between them? I'm using connection pooling with BasicDataSource, or I need an instance of PoolingDataSource to work with connection pooling? Is BasicDataSource thread-safe (can I use it as a Class attribute) or I need to synchronize its access?


回答1:


BasicDataSource is everything for basic needs. It creates internally a PoolableDataSource and an ObjectPool.

PoolableDataSource implements the DataSource interface using a provided ObjectPool. PoolingDataSource take cares of connections and ObjectPool take cares of holding and counting this object.

I would recommend using BasicDataSource. Only, If you really need something special maybe then you can use PoolingDatasource with another implementation of ObjectPool, but it will be very rare and specific.

BasicDataSource is thread-safe, but you should take care to use appropriate accessors rather than accessing protected fields directly to ensure thread-safety.




回答2:


This is more of a (big) supporting comment to ivi's answer above, but am posting it as an answer due to the need for adding snapshots.

BasicDataSource is everything for basic needs. It creates internally a PoolableDataSource and an ObjectPool.

I wanted to look at the code in BasicDataSource to substantiate that statement (which turns out to be true). I hope the following snapshots help future readers.


The following happens when the first time one does a basicDatasource.getConnection(). The first time around the DataSource is created as follows :


  1. This is the raw connectionFactory.

  2. This is the Generic Object Pool ('connectionPool') that is used in the remaining steps.

  3. This combines the above two (connectionFactory + an Object Pool) to create a PoolableConnectionFactory.



    Significantly, during the creation of the PoolableConnectionFactory, the connectionPool is linked with the connectionFactory like so:


  4. Finally, a PoolingDataSource is created from the connectionPool



来源:https://stackoverflow.com/questions/14467480/connection-pooling-with-apache-dbcp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!