Connection Pooling with Apache DBCP

后端 未结 2 1177
别那么骄傲
别那么骄傲 2020-12-24 02:11

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 s

相关标签:
2条回答
  • 2020-12-24 02:49

    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.

    0 讨论(0)
  • 2020-12-24 03:08

    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 :

    enter image description here


    1. This is the raw connectionFactory.

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

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

      Significantly, during the creation of the PoolableConnectionFactory, the connectionPool is linked with the connectionFactory like so:
      enter image description here

    4. Finally, a PoolingDataSource is created from the connectionPool
      enter image description here
    0 讨论(0)
提交回复
热议问题