Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle starvation in apache commons-pool

I am using 1.6 version of apache commons-pool library. As per the javadoc,

whenExhaustedAction specifies the behavior of the borrowObject() method when the pool is exhausted: It can be either WHEN_EXHAUSTED_FAIL, WHEN_EXHAUSTED_GROW, or WHEN_EXHAUSTED_BLOCK.

I want to use the borrowObject and if don't get the object within a specified timeframe, I need some sort of handle to handle the scenario(like i will be rescheduling the tasks, if i don't get the target object)

But the only option, I get here is NoSuchElementException which is a RuntimeException, which I need to catch and handle the error scenario. I am quite sceptical about catching the RuntimeException

Is this the intended way of handling object starvation with GenericObjectPool or do I have any other options ?

like image 432
Albie Morken Avatar asked Dec 18 '25 11:12

Albie Morken


1 Answers

I've looked at the borrowObject documentation and it states that it throws these exceptions

IllegalStateException - after close has been called on this pool.
Exception - when makeObject throws an exception.
NoSuchElementException - when the pool is exhausted and cannot or will not return another instance.

Because NoSuchElementException is a documented behavior of this method, there is nothing wrong with catching it around borrowObject and handling to your liking.

I suggest you catch it right around the call and wrap with one of your own, so if other method in your function throws NoSuchElementException the high level handler is not confused with pool exhaustion. Wrapper exception can be either checked or runtime, depending on your preference and project requirements.

e.g

final T obj;

try
{
  obj = pool.borrowObject( );
}
catch ( NoSuchElementException ex )
{
  throw new MyPoolExhausetdException( ex );
}

// Do something with obj
like image 177
Alexander Pogrebnyak Avatar answered Dec 19 '25 23:12

Alexander Pogrebnyak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!