Tracing borrowed objects
------------------------
Key: POOL-103
URL: https://issues.apache.org/jira/browse/POOL-103
Project: Commons Pool
Issue Type: Improvement
Affects Versions: 1.3
Reporter: falconhand
Once an object is borrowed from a GenericObjectPool, it could be
returnObject()-ed once or invalidateObject()-ed once, but not both.
However, in my working environment, people often tend to write code like this:
MyObject mo = null;
try{
mo = myPool.borrowObject();
}catch(Exception e){
myPool.destoryObject(mo);
}finally{
if(mo != null){
myPool.returnObject(mo);
}
}
In this case, _numActive in GenericObjectPool would be decreased twice once an
Exception is thrown.
So eachtime I use GenericObjectPool, I always wrap the pool like this:
class MyPool extends GenericObjectPool{
HashSet borrowed = new HashSet();
public synchronized Object borrowObject() throws Exception {
Object ret = null;
ret = super.borrowObject();
if (ret != null) {
borrowed.add(ret);
}
return ret;
}
public synchronized void invalidateObject(MyObject mo) {
if(borrowed.contains(mo)){
borrowed.remove(mo);
super.invalidateObject(mo);
}
}
public synchronized void returnObject(Object obj) throws Exception {
if (borrowed.contains(obj)) {
borrowed.remove(obj);
super.returnObject(obj);
}
}
}
So the inner counter _numActive would not get corrupted due to incorrect call
to returnObject() or invalidateObject().
I wonder if this feature could be included into the mighty commons-pool library
to make this class completely FOOL proof :)
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.