Search code examples
javapool

Are there any pooling solutions that allow a key to be passed to the acquire/take method


I have a need to pool connections to some resource. The actual resource is not important for this discussion. The thing of note however is that these connections are not threadsafe, thus the need for pooling as connecting etc is an expensive operation.

My original idea was for one big pool which manages the timing out of connections and kills them etc. However the problem is each connection may include a different parameter, thus when taking the connection it is necessary to pass the parameter and let the pooler use this to attempt to find a connection with the same value. If it (the pool) could not find the object it then needs to create an instance passing the parameter of course etc.

However Apache Commons Pools and ohter similar libraries do not have any provision for this sort of concept. Are there any libraries which include this sort of concept. I suppose underneath the Pool holds a list of instances and when a take request comes in it uses some matcher to loop over its available objects and returns the first that is matched.

Note it is not possible to chnage the parameter of the connection object thus the problem and the reason for the question being asked.

  • Is there a better way to solve this problem ?
  • Is there a pooling library that does the above ?
  • Should i just write my own ?

Solution

  • Could you check KeyedObjectPool in apache Pool?