org.apache.commons.pool
Interface ObjectPool

All Known Implementing Classes:
BaseObjectPool, GenericObjectPool, SoftReferenceObjectPool, StackObjectPool

public interface ObjectPool

A pooling interface.

ObjectPool defines a trivially simple pooling interface. The only required methods are borrowObject and returnObject.

Example of use:
 Object obj = null;
 
 try {
    obj = pool.borrowObject();
    //...use the object...
 } catch(Exception e) {
    //...handle any exceptions...
 } finally {
    // make sure the object is returned to the pool
    if(null != obj) {
       pool.returnObject(obj);
    }
 }
See BaseObjectPool for a simple base implementation.

Version:
$Revision: 1.11 $ $Date: 2004/02/28 11:46:33 $

Method Summary
 void addObject()
          Create an object using my factory or other implementation dependent mechanism, and place it into the pool.
 Object borrowObject()
          Obtain an instance from my pool.
 void clear()
          Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).
 void close()
          Close this pool, and free any resources associated with it.
 int getNumActive()
          Return the number of instances currently borrowed from my pool (optional operation).
 int getNumIdle()
          Return the number of instances currently idle in my pool (optional operation).
 void invalidateObject(Object obj)
          Invalidates an object from the pool By contract, obj MUST have been obtained using borrowObject or a related method as defined in an implementation or sub-interface.
 void returnObject(Object obj)
          Return an instance to my pool.
 void setFactory(org.apache.commons.pool.PoolableObjectFactory factory)
          Sets the factory I use to create new instances (optional operation).
 

Method Detail

borrowObject

public Object borrowObject()
                    throws Exception
Obtain an instance from my pool. By contract, clients MUST return the borrowed instance using returnObject or a related method as defined in an implementation or sub-interface.

The behaviour of this method when the pool has been exhausted is not specified (although it may be specified by implementations).

Returns:
an instance from my pool.
Exception

returnObject

public void returnObject(Object obj)
                  throws Exception
Return an instance to my pool. By contract, obj MUST have been obtained using borrowObject or a related method as defined in an implementation or sub-interface.

Parameters:
obj - a borrowed instance to be returned.
Exception

invalidateObject

public void invalidateObject(Object obj)
                      throws Exception
Invalidates an object from the pool By contract, obj MUST have been obtained using borrowObject or a related method as defined in an implementation or sub-interface.

This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid. If the connection should be validated before or after borrowing, then the PoolableObjectFactory.validateObject(java.lang.Object) method should be used instead.

Parameters:
obj - a borrowed instance to be returned.
Exception

addObject

public void addObject()
               throws Exception
Create an object using my factory or other implementation dependent mechanism, and place it into the pool. addObject() is useful for "pre-loading" a pool with idle objects. (Optional operation).

Exception

getNumIdle

public int getNumIdle()
               throws UnsupportedOperationException
Return the number of instances currently idle in my pool (optional operation). This may be considered an approximation of the number of objects that can be borrowed without creating any new instances.

Returns:
the number of instances currently idle in my pool
Throws:
UnsupportedOperationException - if this implementation does not support the operation

getNumActive

public int getNumActive()
                 throws UnsupportedOperationException
Return the number of instances currently borrowed from my pool (optional operation).

Returns:
the number of instances currently borrowed in my pool
Throws:
UnsupportedOperationException - if this implementation does not support the operation

clear

public void clear()
           throws Exception,
                  UnsupportedOperationException
Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).

Throws:
UnsupportedOperationException - if this implementation does not support the operation
Exception

close

public void close()
           throws Exception
Close this pool, and free any resources associated with it.

Exception

setFactory

public void setFactory(org.apache.commons.pool.PoolableObjectFactory factory)
                throws IllegalStateException,
                       UnsupportedOperationException
Sets the factory I use to create new instances (optional operation).

Parameters:
factory - the PoolableObjectFactory I use to create new instances.
Throws:
IllegalStateException - when the factory cannot be set at this time
UnsupportedOperationException - if this implementation does not support the operation


Copyright © 2001-2004 The Apache Software Foundation. All Rights Reserved.