开发者

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

I have a need to pool conn开发者_如何转开发ections 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 ?


Why not use the key to find the pool first? Then use one of the available solutions. I don't think you would be simplifying anything by trying to pool everything together since differently-keyed items are not cross-compatible.


Could you check KeyedObjectPool in apache Pool?


If the object is not thread-safe, why pooling? Your pool manager won't be able to check the status / close them from other thread anyway.

Just keep one copy in each thread using ThreadLocal.


KeyedObjectPool is the answer and if you want to see an example have a look at this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜