commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Thomas (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (POOL-103) Tracing borrowed objects
Date Wed, 23 Mar 2011 19:24:05 GMT

     [ https://issues.apache.org/jira/browse/POOL-103?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Mark Thomas updated POOL-103:
-----------------------------

    Fix Version/s:     (was: 3.0)
                   2.0

Move to 2.0.x

> 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: immars
>             Fix For: 2.0
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira

Mime
View raw message