springspring-aopapache-commons-pool

What does CommonsPool2TargetSource do if you disable blockWhenExhausted?


The deprecated CommonPoolTargetSource had a set of behaviors you could define using setWhenExhaustedActionName(). The CommonsPool2TargetSource and the CommonsPool2 BaseGenericObjectPool it wraps describe "getBlockWhenExhausted" as

Returns whether to block when the borrowObject() method is invoked when the pool is exhausted

I figured that much out! What does this do otherwise?

Does the Pool get expanded as in when you set CommonsPool to "WHEN_EXHAUSTED_GROW" or does it just throw an exception? What happens?


Solution

  • As stated in the documentation of ObjectPool.borrowObject() behavior when exhausted is implementation depended.

    The behaviour of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations).

    The GenericObjectPool implementation supplied with CommonsPool2 will block if getBlockWhenExhausted() is true and immediately throw a NoSuchElementException otherwise. (as documented in GenericObjectPoolborrowObject(long borrowMaxWaitMillis))

    If the pool is exhausted (no available idle instances and no capacity to create new ones), this method will either block (if BaseGenericObjectPool.getBlockWhenExhausted() is true) or throw a NoSuchElementException (if BaseGenericObjectPool.getBlockWhenExhausted() is false). The length of time that this method will block when BaseGenericObjectPool.getBlockWhenExhausted() is true is determined by the value passed in to the borrowMaxWaitMillis parameter.

    A similar behavior to "WHEN_EXHAUSTED_GROW" is not available and is most likely reached by setting maxTotal to -1 so the pool could not exhaust at all.