Connection Pooling with Apache DBCP

醉酒当歌 提交于 2019-11-29 22:22:41
ivi

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.

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

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