On Friday 06 September 2002 7:31 am, Waldhoff, Rodney wrote:
>By the way, I accidentally replied directly to John on this (instead of the
>list).  We seem to be working it out off-list.  John, feel free to forward
>our previous thread, or reply to it to the list.  Writing you directly was an
>oversight.


----------  Forwarded Message  ----------

1. We create a new LmsPage, which in turn creates a new StackObjectPool
(pooling LmsServerMap objects)

2. At some point (not in your listing) we invoke LmsPage.getServerMap.  The
first time this method is invoked, we borrow an object from the pool, and
store a instance-scope reference to it.  Every other invocation of that
method returns the previously borrowed LmsServerMap.

3. Eventually, the JSP page completes, the LmsPage bean goes out of scope,
finalize is invoked and the LmsServerMap is returned to the pool.

In other words, we never borrow more than one LmsServerMap from any pool
instance.

In this scenario, the behavior is essentially the same as if you replaced
LmsPage.getServerMap with:

  public ServerMap getServerMap() throws Exception {
    if( serverMap == null )
    {
      serverMap = new LmsServerMap();
    }
    return serverMap;
  }

We're not really using pooling at all.


I think maybe what you're trying to do could be accomplished like this:

public class LmsPage extends Page
{
  protected static ObjectPool serverMapPool;
  static
  {
    PoolableObjectFactory serverMapFactory = new LmsServerMapFactory();
    serverMapPool = new StackObjectPool( serverMapFactory, 1000 );
  }

  private ServerMap serverMap = null;

  public LmsPage()
  {
  }

  public ServerMap getServerMap() throws Exception
  {
    if( serverMap == null )
    {
      serverMap = (ServerMap)serverMapPool.borrowObject();
    }
    return serverMap;
  }

  public void finalize() throws Throwable
  {
    super.finalize();
    if( serverMap != null )
    {
      serverMapPool.returnObject( serverMap );
    }
  }
}

I.e., make the serverMapPool a static (class-scope) variable.  Then each
instance of LmsPage will still only borrow (and cache) one serverMap
instance, but when one LmsPage instance goes out of scope (and gets
finalized), it will return this cached instance to the pool.  Later
instances of LmsPage can then reuse that instnace LmsServerMap by borrowing
it from their shared pool.

I'd also double check the semantics of finalize.  I think this approach
(return on finalize) should work, but sometimes finalize is called when an
instance is in a funny, half-gc'ed state.

-----Original Message-----
From: John Walstra [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 06, 2002 7:45 AM
To: Waldhoff, Rodney
Subject: Re: Object pooling

On Friday 06 September 2002 7:31 am, Waldhoff, Rodney wrote:
> Hi John,
>
> No special JNDI stuff is needed to use commons-pool, it's just
> pool.borrowObject() and then later pool.returnObject(object).
>
> It could be a configuration problem, or a commons-pool bug revealed by
> specific configuration (we fixed one of those yesterday), or perhaps just

a

> misunderstanding about the API.
>
> Can you provide more detail?
>  -Rod

Thanks for the response. Here is what I am doing.

First I have my class called LmsServerMap.class. Basically it's a bean that
reads information out of an XML file and puts it into a Hashtable.

Secondly I have a Factory class called LmsServerMapFactory.class which is
basically

--LmsServerMapFactory.class
package lms.prod.bean;

import java.io.*;
import java.lang.*;
import org.apache.commons.pool.*;
import lms.prod.bean.LmsServerMap;

public class LmsServerMapFactory extends BasePoolableObjectFactory
{
  public Object makeObject() throws Exception
  {
    return new LmsServerMap();
  }

  public void passivateObject(Object obj) throws Exception
  {
    LmsServerMap map = (LmsServerMap)obj;
    map.clear();
  }
}
--

I then use it in a third class which is use used as a bean in the JSP page.

--LmsPage.class (cut and paste of LmsPage and Page )
package lms.prod.bean.page;

import org.apache.commons.pool.*;
import org.apache.commons.pool.impl.*;
import lms.core.ServerMap;
import lms.core.page.Page;
import lms.prod.bean.LmsServerMapFactory;

public class LmsPage extends Page
{
  protected ObjectPool serverMapPool;
  private ServerMap serverMap = null;

  public LmsPage()
  {
    PoolableObjectFactory serverMapFactory = new LmsServerMapFactory();
    serverMapPool = new StackObjectPool( serverMapFactory, 1000 );
  }

  public  ServerMap getServerMap() throws Exception
  {
    if( serverMap == null )
    {
      serverMap = (ServerMap)serverMapPool.borrowObject();
    }
    return serverMap;
  }

  public void finalize() throws Throwable
  {
    super.finalize();
    if( serverMap != null )
    {
      serverMapPool.returnObject( serverMap );
    }
  }
}
--

Then in the JSP page I put ...

<%@ page language='java' contentType='text/html' %>
<%@ taglib uri="/lms" prefix="lms" %>
<jsp:useBean id='pg' class='lms.prod.bean.page.LmsPage'/>

I have not done anything to my server.xml or context web.xml for the
pooling.

Thanks,
John

--
John Walstra
[EMAIL PROTECTED]

Operators killed when huge stack of backup tapes fell over.

-------------------------------------------------------


-- 
John Walstra
1002 North Stanford Street
Port Washington, WI 53074
H: (262) 284-2395
C: (847) 858-2395

[EMAIL PROTECTED]
[EMAIL PROTECTED]
[EMAIL PROTECTED]

How come everyone's going so slow if it's called rush hour?

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to