You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by kpetrov <KV...@OwnMail.net> on 2008/06/03 01:44:55 UTC

[POOL] you can return your object several times?

I tested a simple scenario where I return the same object several times:
		PoolableJmsTemplateFactory pjtf = new PoolableJmsTemplateFactory();
		pjtf.setConnectionFactory(connectionFactory);
		ObjectPool pool = new SoftReferenceObjectPool(pjtf);
		System.out.println("Idle templates:"+pool.getNumIdle());
		JmsTemplate jmsTemplate = (JmsTemplate)pool.borrowObject();
		System.out.println("Active templates:"+pool.getNumActive());
		JmsTemplate jmsTemplate2 = (JmsTemplate)pool.borrowObject();
		System.out.println("Active templates:"+pool.getNumActive());
		pool.returnObject(jmsTemplate);
		System.out.println("Active templates:"+pool.getNumActive());
		pool.returnObject(jmsTemplate2);
		System.out.println("Active templates:"+pool.getNumActive());
		System.out.println("Idle templates:"+pool.getNumIdle());
		pool.returnObject(jmsTemplate2);
		pool.returnObject(jmsTemplate2);
		System.out.println("Active templates:"+pool.getNumActive());
		System.out.println("Idle templates:"+pool.getNumIdle());

What I get is a very unpleasant result:

Idle templates:0
Active templates:1
Active templates:2
Active templates:1
Active templates:0
Idle templates:2
Active templates:-2
Idle templates:4

The pool let me return the object several times!
Shouldn't it throw an exception or at least not have negative number of
active objects?

On top of that, when I tried to borrow objects again the same pool, the same
object was returned several times. This is even worse than the negative
number of active objects.

Why is it implemented in such a way?
-- 
View this message in context: http://www.nabble.com/-POOL--you-can-return-your-object-several-times--tp17613295p17613295.html
Sent from the Commons - User mailing list archive at Nabble.com.


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


Re: [POOL] you can return your object several times?

Posted by Phil Steitz <ph...@steitz.com>.
sebb wrote:
> On 04/06/2008, Mark Thomas <ma...@apache.org> wrote:
>   
>>  sebb wrote:
>>
>>     
>>> On 03/06/2008, Mark Thomas <ma...@apache.org> wrote:
>>>
>>>       
>>>>  kpetrov wrote:
>>>>
>>>>
>>>>         
>>>>> Mark Thomas-18 wrote:
>>>>>
>>>>>
>>>>>           
>>>>>> kpetrov wrote:
>>>>>>
>>>>>>
>>>>>>             
>>>>>>> I tested a simple scenario where I return the same object several
>>>>>>>
>>>>>>>               
>>>> times:
>>>>
>>>>         
>>>>>> Which version of pool are you using?
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>             
>>>>> 1.4. Actually, just checked the source code...seems like this is done
>>>>>           
>> by
>>     
>>>>> design... was surprised though.
>>>>>
>>>>>
>>>>>           
>>> So long as it is documented, it seems reasonable to me.
>>>
>>>       
>>  I may not be understanding the code correctly but it looks like only
>> GenericObjectPool has a warning on returnObject() but other implementations
>> have the same contract but no warning. A patch that updated the JavaDocs
>> wouldn't hurt.
>>
>>
>>     
>>> For example, some close() methods can be called multiple times; only
>>> the first close() needs to do anything, later calls to close() are
>>> ignored. This can be useful in cleanup code.
>>>
>>>       
>>  I should look at the code some more. I am wondering what happens when
>> threadA calls close() (which in turn calls returnObject()) , threadB calls
>> borrowObject() and gets the object just returned by threadA and whilst
>> threadB is using the object threadA calls close() again.
>>
>>
>>     
>>>>  I haven't looked at the code in a while so I don't know how easy it
>>>>         
>> would
>>     
>>>> be to patch this. If you were able to suggest a patch I am sure it would
>>>>         
>> be
>>     
>>>> welcomed (create a JIRA issue and attach it there).
>>>>
>>>>         
>>> I don't think the behaviour should be changed unless it is clearly not
>>>       
>> intended.
>>     
>>> And even then, unless it causes a problem, why risk breaking
>>> applications that rely on it?
>>>
>>>       
>>  I can't think of any use cases where having the same object in the pool
>> more than once is anything other than a bug. I would be interested in any
>> use case you have that relies on this behaviour.
>>     
>
> Nor can I, and the code should not allow that to happen.
> It should either treat the second call as a noop, or throw an exception.
>
> But allowing the application to returnObject() more than once may
> perhaps be useful, as in the close() analogy.
>
>   
>>  The complexity of the code necessary to enforce the contract stated in the
>> Javadocs would have to be weighed against the benefits of explicitly
>> enforcing the contract. I can see some benefit in doing this but until there
>> is an actual patch to consider...
>>
>>     
The practical problem here is that you would need to fully search the 
pool on each returnObject.  This could cause performance problems and 
would be tricky to do safely.  Also, I need some convincing that it is a 
good idea for the pool to make any commitments regarding client 
references to objects that have been returned - i.e., allowing clients 
to continue to hold and meaningfully use these references (which 
returning them twice basically gets into).  

I assume also that reference equality, not equals would be used to 
compare; otherwise some legitimate use cases could break.

If this behavior is really important to client applications, it is 
probably better on the client side to wrap the pooled objects in an 
adaptor that makes "close" or "return" idempotent, which is what DBCP 
does (in trunk repeated close is now allowed). 

Phil


>>  Mark
>>
>>
>> ---------------------------------------------------------------------
>>  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
>
>   


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


Re: [POOL] you can return your object several times?

Posted by sebb <se...@gmail.com>.
On 04/06/2008, Mark Thomas <ma...@apache.org> wrote:
>
>  sebb wrote:
>
> > On 03/06/2008, Mark Thomas <ma...@apache.org> wrote:
> >
> > >  kpetrov wrote:
> > >
> > >
> > > >
> > > >
> > > > Mark Thomas-18 wrote:
> > > >
> > > >
> > > > > kpetrov wrote:
> > > > >
> > > > >
> > > > > > I tested a simple scenario where I return the same object several
> > > > > >
> > > > >
> > > >
> > > times:
> > >
> > > >
> > > > > Which version of pool are you using?
> > > > >
> > > > >
> > > > >
> > > > >
> > > > 1.4. Actually, just checked the source code...seems like this is done
> by
> > > > design... was surprised though.
> > > >
> > > >
> > >
> >
> > So long as it is documented, it seems reasonable to me.
> >
>  I may not be understanding the code correctly but it looks like only
> GenericObjectPool has a warning on returnObject() but other implementations
> have the same contract but no warning. A patch that updated the JavaDocs
> wouldn't hurt.
>
>
> > For example, some close() methods can be called multiple times; only
> > the first close() needs to do anything, later calls to close() are
> > ignored. This can be useful in cleanup code.
> >
>  I should look at the code some more. I am wondering what happens when
> threadA calls close() (which in turn calls returnObject()) , threadB calls
> borrowObject() and gets the object just returned by threadA and whilst
> threadB is using the object threadA calls close() again.
>
>
> >
> > >  I haven't looked at the code in a while so I don't know how easy it
> would
> > > be to patch this. If you were able to suggest a patch I am sure it would
> be
> > > welcomed (create a JIRA issue and attach it there).
> > >
> >
> > I don't think the behaviour should be changed unless it is clearly not
> intended.
> > And even then, unless it causes a problem, why risk breaking
> > applications that rely on it?
> >
>  I can't think of any use cases where having the same object in the pool
> more than once is anything other than a bug. I would be interested in any
> use case you have that relies on this behaviour.

Nor can I, and the code should not allow that to happen.
It should either treat the second call as a noop, or throw an exception.

But allowing the application to returnObject() more than once may
perhaps be useful, as in the close() analogy.

>  The complexity of the code necessary to enforce the contract stated in the
> Javadocs would have to be weighed against the benefits of explicitly
> enforcing the contract. I can see some benefit in doing this but until there
> is an actual patch to consider...
>
>
>  Mark
>
>
> ---------------------------------------------------------------------
>  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] you can return your object several times?

Posted by Mark Thomas <ma...@apache.org>.
sebb wrote:
> On 03/06/2008, Mark Thomas <ma...@apache.org> wrote:
>>  kpetrov wrote:
>>
>>>
>>>
>>> Mark Thomas-18 wrote:
>>>
>>>> kpetrov wrote:
>>>>
>>>>> I tested a simple scenario where I return the same object several
>> times:
>>>> Which version of pool are you using?
>>>>
>>>>
>>>>
>>> 1.4. Actually, just checked the source code...seems like this is done by
>>> design... was surprised though.
>>>
> 
> So long as it is documented, it seems reasonable to me.
I may not be understanding the code correctly but it looks like only 
GenericObjectPool has a warning on returnObject() but other implementations 
have the same contract but no warning. A patch that updated the JavaDocs 
wouldn't hurt.

> For example, some close() methods can be called multiple times; only
> the first close() needs to do anything, later calls to close() are
> ignored. This can be useful in cleanup code.
I should look at the code some more. I am wondering what happens when 
threadA calls close() (which in turn calls returnObject()) , threadB calls 
borrowObject() and gets the object just returned by threadA and whilst 
threadB is using the object threadA calls close() again.

>>  I haven't looked at the code in a while so I don't know how easy it would
>> be to patch this. If you were able to suggest a patch I am sure it would be
>> welcomed (create a JIRA issue and attach it there).
> 
> I don't think the behaviour should be changed unless it is clearly not intended.
> And even then, unless it causes a problem, why risk breaking
> applications that rely on it?
I can't think of any use cases where having the same object in the pool 
more than once is anything other than a bug. I would be interested in any 
use case you have that relies on this behaviour.

The complexity of the code necessary to enforce the contract stated in the 
Javadocs would have to be weighed against the benefits of explicitly 
enforcing the contract. I can see some benefit in doing this but until 
there is an actual patch to consider...

Mark


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


Re: [POOL] you can return your object several times?

Posted by sebb <se...@gmail.com>.
On 03/06/2008, Mark Thomas <ma...@apache.org> wrote:
>
>  kpetrov wrote:
>
> >
> >
> >
> > Mark Thomas-18 wrote:
> >
> > >
> > > kpetrov wrote:
> > >
> > > > I tested a simple scenario where I return the same object several
> times:
> > > >
> > > Which version of pool are you using?
> > >
> > >
> > >
> >
> > 1.4. Actually, just checked the source code...seems like this is done by
> > design... was surprised though.
> >
>

So long as it is documented, it seems reasonable to me.

For example, some close() methods can be called multiple times; only
the first close() needs to do anything, later calls to close() are
ignored. This can be useful in cleanup code.

>  I haven't looked at the code in a while so I don't know how easy it would
> be to patch this. If you were able to suggest a patch I am sure it would be
> welcomed (create a JIRA issue and attach it there).

I don't think the behaviour should be changed unless it is clearly not intended.
And even then, unless it causes a problem, why risk breaking
applications that rely on it?

>  Mark
>
>
> ---------------------------------------------------------------------
>  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] you can return your object several times?

Posted by Mark Thomas <ma...@apache.org>.
kpetrov wrote:
> 
> 
> 
> Mark Thomas-18 wrote:
>>
>> kpetrov wrote:
>>> I tested a simple scenario where I return the same object several times:
>> Which version of pool are you using?
>>
>>
> 
> 1.4. Actually, just checked the source code...seems like this is done by
> design... was surprised though.

I haven't looked at the code in a while so I don't know how easy it would 
be to patch this. If you were able to suggest a patch I am sure it would be 
welcomed (create a JIRA issue and attach it there).

Mark

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


Re: [POOL] you can return your object several times?

Posted by kpetrov <KV...@OwnMail.net>.


Mark Thomas-18 wrote:
> 
> 
> kpetrov wrote:
>> 
>> I tested a simple scenario where I return the same object several times:
> 
> Which version of pool are you using?
> 
> 

1.4. Actually, just checked the source code...seems like this is done by
design... was surprised though.
-- 
View this message in context: http://www.nabble.com/-POOL--you-can-return-your-object-several-times--tp17613295p17626681.html
Sent from the Commons - User mailing list archive at Nabble.com.


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


Re: [POOL] you can return your object several times?

Posted by Mark Thomas <ma...@apache.org>.
kpetrov wrote:
> 
> I tested a simple scenario where I return the same object several times:

Which version of pool are you using?

Mark


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


Re: [POOL] you can return your object several times?

Posted by Phil Steitz <ph...@steitz.com>.
kpetrov wrote:
> I tested a simple scenario where I return the same object several times:
> 		PoolableJmsTemplateFactory pjtf = new PoolableJmsTemplateFactory();
> 		pjtf.setConnectionFactory(connectionFactory);
> 		ObjectPool pool = new SoftReferenceObjectPool(pjtf);
> 		System.out.println("Idle templates:"+pool.getNumIdle());
> 		JmsTemplate jmsTemplate = (JmsTemplate)pool.borrowObject();
> 		System.out.println("Active templates:"+pool.getNumActive());
> 		JmsTemplate jmsTemplate2 = (JmsTemplate)pool.borrowObject();
> 		System.out.println("Active templates:"+pool.getNumActive());
> 		pool.returnObject(jmsTemplate);
> 		System.out.println("Active templates:"+pool.getNumActive());
> 		pool.returnObject(jmsTemplate2);
> 		System.out.println("Active templates:"+pool.getNumActive());
> 		System.out.println("Idle templates:"+pool.getNumIdle());
> 		pool.returnObject(jmsTemplate2);
> 		pool.returnObject(jmsTemplate2);
> 		System.out.println("Active templates:"+pool.getNumActive());
> 		System.out.println("Idle templates:"+pool.getNumIdle());
>
> What I get is a very unpleasant result:
>
> Idle templates:0
> Active templates:1
> Active templates:2
> Active templates:1
> Active templates:0
> Idle templates:2
> Active templates:-2
> Idle templates:4
>
> The pool let me return the object several times!
> Shouldn't it throw an exception or at least not have negative number of
> active objects?
>
> On top of that, when I tried to borrow objects again the same pool, the same
> object was returned several times. This is even worse than the negative
> number of active objects.
>
> Why is it implemented in such a way?
>   
Returning the same object twice in sequence violates the contract of the 
pool interface.  Validity of counters also depends on following the 
borrow-return contract.

Phil


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