[jira] [Resolved] (POOL-103) Tracing borrowed objects

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Resolved] (POOL-103) Tracing borrowed objects

Gary D. Gregory (Jira)

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

Mark Thomas resolved POOL-103.
------------------------------

    Resolution: Fixed

The re-factoring in pool2 has added checks that throw an exception if:
- an object is returned to the pool that wasn't borrowed from the pool
- an object is returned to the pool twice

> 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