You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Noriyuki Torii <to...@gmail.com> on 2014/09/03 18:27:44 UTC

[pool] Validation code invoked on unexpected timing.

Hi, all.

I found BasePooledObjectFactory.validateObject() of commons-pool
sometimes called
on unexpected timing and I cannot identify the cause.
So I would like to ask some advice.

I've configured the pool so as to the validateObject() would be
invoked only on instance
creation, but sometimes it was invoked for "non-fresh" instance on borrowing.

I also attach small reproduction code below.
Can anyone tell it is because some lack of configurations, or, actual
pool's bug?

Thanks in advance.
----
Noriyuki Torii

----
import java.io.*;
import java.util.*;

import org.apache.commons.pool2.*;
import org.apache.commons.pool2.impl.*;

public class reproduction
{
    private static class MyPooledObj
    {
        volatile int i = 0;
        final String uuid = UUID.randomUUID().toString();
    }

    private static class MyFactory extends BasePooledObjectFactory<MyPooledObj>
    {
        @Override
        public MyPooledObj create() {
            return new MyPooledObj();
        }

        @Override
        public PooledObject<MyPooledObj> wrap(MyPooledObj o) {
            return new DefaultPooledObject<MyPooledObj>(o);
        }

        @Override
        public boolean validateObject(PooledObject<MyPooledObj> o) {
            MyPooledObj myobj = o.getObject();
            synchronized (myobj) {
                myobj.i++;
            }
            System.out.println("Validated.");
            return true;
        }
    }

    public static void main(String[] args)
        throws Exception
    {
        GenericObjectPoolConfig poolCfg = new GenericObjectPoolConfig() ;
        poolCfg.setMaxTotal(1);
        poolCfg.setMaxIdle(1);
        poolCfg.setBlockWhenExhausted(true);
        poolCfg.setMaxWaitMillis(10000);

        poolCfg.setTestOnCreate(true); // should be validated only on creation
        poolCfg.setTestOnBorrow(false);
        poolCfg.setTestOnReturn(false);
        poolCfg.setTestWhileIdle(false);

        final GenericObjectPool<MyPooledObj> pool =
            new GenericObjectPool<MyPooledObj>(new MyFactory(), poolCfg);

        final MyPooledObj o = pool.borrowObject();
        System.out.printf("%d: %s\n", o.i, o.uuid);

        Timer t = new Timer();
        t.schedule
            (new TimerTask() {
                    public void run() {
                        pool.returnObject(o);
                    }
                }, 3000);

        // validation will occur again for non-fresh instance,
        // confirmed on commons-pool2-2.2 with jdk1.7.0_55
        MyPooledObj o2 = pool.borrowObject();
        System.out.printf("%d: %s\n", o2.i, o2.uuid);
        pool.returnObject(o2);
        pool.close();

        t.cancel();
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [POOL-276] Validation code invoked on unexpected timing.

Posted by Bernd Eckenfels <ec...@zusammenkunft.net>.
Hello,

your JIRA report looks good! I took the liberty to modify
it a bit and included JIRA markup {code} before and after the code, so
it does do formatting and syntax highliting.

BTW: I added some more print in my local version of your reproducer and
adding the thread name, so it is clear when things are happening. The
output reproduces the following sequence:

1409804599167 main                    Created 0 379cbef5-3e40-4f39-af5f-ac3e76506e81
1409804599229 main                    Wrapped 0 379cbef5-3e40-4f39-af5f-ac3e76506e81
1409804599229 main                  Validated 1 379cbef5-3e40-4f39-af5f-ac3e76506e81
1409804599229 main                   Borrowed 1 379cbef5-3e40-4f39-af5f-ac3e76506e81
1409804602256 Timer-0               Returning 1 379cbef5-3e40-4f39-af5f-ac3e76506e81
1409804602256 main                  Validated 2 379cbef5-3e40-4f39-af5f-ac3e76506e81
1409804602256 main             Borrowed Again 2 379cbef5-3e40-4f39-af5f-ac3e76506e81

Just in case anybody wondered, it is happening after the return in the borrow thread.

Gruss
Bernd


 Am Thu, 4 Sep 2014 12:27:39
+0900 schrieb Noriyuki Torii <to...@gmail.com>:

> Hi, Phil.
> Thanks for your response.
> 
> I opened a ticket for this issue as POOL-276.
> In fact, I am a newbie of JIRA.  So if there are any mistakes on this
> ticket, please let me know.
> 
> Regards,
> 
> 2014-09-04 10:35 GMT+09:00 Phil Steitz <ph...@gmail.com>:
> > On 9/3/14 9:27 AM, Noriyuki Torii wrote:
> >> Hi, all.
> >>
> >> I found BasePooledObjectFactory.validateObject() of commons-pool
> >> sometimes called
> >> on unexpected timing and I cannot identify the cause.
> >> So I would like to ask some advice.
> >>
> >> I've configured the pool so as to the validateObject() would be
> >> invoked only on instance
> >> creation, but sometimes it was invoked for "non-fresh" instance on
> >> borrowing.
> >>
> >> I also attach small reproduction code below.
> >> Can anyone tell it is because some lack of configurations, or,
> >> actual pool's bug?
> >
> > Thanks for reporting this.  Looks like it could be a pool bug.  Do
> > you mind opening a JIRA ticket for this?
> >
> > Phil
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [pool] Validation code invoked on unexpected timing.

Posted by Noriyuki Torii <to...@gmail.com>.
Hi, Phil.
Thanks for your response.

I opened a ticket for this issue as POOL-276.
In fact, I am a newbie of JIRA.  So if there are any mistakes on this
ticket, please let me know.

Regards,

2014-09-04 10:35 GMT+09:00 Phil Steitz <ph...@gmail.com>:
> On 9/3/14 9:27 AM, Noriyuki Torii wrote:
>> Hi, all.
>>
>> I found BasePooledObjectFactory.validateObject() of commons-pool
>> sometimes called
>> on unexpected timing and I cannot identify the cause.
>> So I would like to ask some advice.
>>
>> I've configured the pool so as to the validateObject() would be
>> invoked only on instance
>> creation, but sometimes it was invoked for "non-fresh" instance on borrowing.
>>
>> I also attach small reproduction code below.
>> Can anyone tell it is because some lack of configurations, or, actual
>> pool's bug?
>
> Thanks for reporting this.  Looks like it could be a pool bug.  Do
> you mind opening a JIRA ticket for this?
>
> Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [pool] Validation code invoked on unexpected timing.

Posted by Phil Steitz <ph...@gmail.com>.
On 9/3/14 9:27 AM, Noriyuki Torii wrote:
> Hi, all.
>
> I found BasePooledObjectFactory.validateObject() of commons-pool
> sometimes called
> on unexpected timing and I cannot identify the cause.
> So I would like to ask some advice.
>
> I've configured the pool so as to the validateObject() would be
> invoked only on instance
> creation, but sometimes it was invoked for "non-fresh" instance on borrowing.
>
> I also attach small reproduction code below.
> Can anyone tell it is because some lack of configurations, or, actual
> pool's bug?

Thanks for reporting this.  Looks like it could be a pool bug.  Do
you mind opening a JIRA ticket for this?

Phil
>
> Thanks in advance.
> ----
> Noriyuki Torii
>
> ----
> import java.io.*;
> import java.util.*;
>
> import org.apache.commons.pool2.*;
> import org.apache.commons.pool2.impl.*;
>
> public class reproduction
> {
>     private static class MyPooledObj
>     {
>         volatile int i = 0;
>         final String uuid = UUID.randomUUID().toString();
>     }
>
>     private static class MyFactory extends BasePooledObjectFactory<MyPooledObj>
>     {
>         @Override
>         public MyPooledObj create() {
>             return new MyPooledObj();
>         }
>
>         @Override
>         public PooledObject<MyPooledObj> wrap(MyPooledObj o) {
>             return new DefaultPooledObject<MyPooledObj>(o);
>         }
>
>         @Override
>         public boolean validateObject(PooledObject<MyPooledObj> o) {
>             MyPooledObj myobj = o.getObject();
>             synchronized (myobj) {
>                 myobj.i++;
>             }
>             System.out.println("Validated.");
>             return true;
>         }
>     }
>
>     public static void main(String[] args)
>         throws Exception
>     {
>         GenericObjectPoolConfig poolCfg = new GenericObjectPoolConfig() ;
>         poolCfg.setMaxTotal(1);
>         poolCfg.setMaxIdle(1);
>         poolCfg.setBlockWhenExhausted(true);
>         poolCfg.setMaxWaitMillis(10000);
>
>         poolCfg.setTestOnCreate(true); // should be validated only on creation
>         poolCfg.setTestOnBorrow(false);
>         poolCfg.setTestOnReturn(false);
>         poolCfg.setTestWhileIdle(false);
>
>         final GenericObjectPool<MyPooledObj> pool =
>             new GenericObjectPool<MyPooledObj>(new MyFactory(), poolCfg);
>
>         final MyPooledObj o = pool.borrowObject();
>         System.out.printf("%d: %s\n", o.i, o.uuid);
>
>         Timer t = new Timer();
>         t.schedule
>             (new TimerTask() {
>                     public void run() {
>                         pool.returnObject(o);
>                     }
>                 }, 3000);
>
>         // validation will occur again for non-fresh instance,
>         // confirmed on commons-pool2-2.2 with jdk1.7.0_55
>         MyPooledObj o2 = pool.borrowObject();
>         System.out.printf("%d: %s\n", o2.i, o2.uuid);
>         pool.returnObject(o2);
>         pool.close();
>
>         t.cancel();
>     }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org