The most common way to use ResourcePool is to subclass it and override the methods createResource and destroyResource. The createResource method creates a new instance of your expensive resource. The destroyResource method is called when the resource pool decides that the resource is no longer needed, and is called to give you a chance to perform any cleanup procedures required for that resource.

You also have the option of overriding verifyResourceValidity. This is called to make sure that a resource is still available for use—for example, it can detect if a previously opened connection was closed since the last time it was used.

The following example shows how one might subclass ResourcePool:

import atg.service.resourcepool.*;

public class MyPool extends ResourcePool {
  public MyPool () { }

  public Object createResource () throws ResourcePoolException {
    return new ReallyExpensiveObject ();
  }

  public void destroyResource (Object resource) {
    ((ReallyExpensiveObject) resource).close ();
  }
}

Notice that createResource throws ResourcePoolException. If your object creation procedure results in an error, you must throw a ResourcePoolException to report that error.


Copyright © 1997, 2012 Oracle and/or its affiliates. All rights reserved.

Legal Notices