org.apache.tomcat.dbcp.pool.KeyedObjectPool Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.dbcp.pool;
import java.util.NoSuchElementException;
/**
* A "keyed" pooling interface.
*
* A keyed pool pools instances of multiple types. Each
* type may be accessed using an arbitrary key.
*
*
* Example of use:
*
Object obj = null
;
* Object key = "Key"
;
*
* try
{
* obj = pool.borrowObject(key);
* //...use the object...
* } catch
(Exception e) {
* // invalidate the object
* pool.invalidateObject(key, obj);
* // do not return the object to the pool twice
* obj = null
;
* } finally
{
* // make sure the object is returned to the pool
* if
(null
!= obj) {
* pool.returnObject(key, obj);
* }
* }
*
*
* {@link KeyedObjectPool} implementations may choose to store at most
* one instance per key value, or may choose to maintain a pool of instances
* for each key (essentially creating a {@link java.util.Map Map} of
* {@link ObjectPool pools}).
*
*
* See {@link BaseKeyedObjectPool} for a simple base implementation.
*
* @author Rodney Waldhoff
* @author Sandy McArthur
* @version $Revision: 962893 $ $Date: 2010-07-10 13:37:27 -0400 (Sat, 10 Jul 2010) $
* @see KeyedPoolableObjectFactory
* @see KeyedObjectPoolFactory
* @see ObjectPool
* @see BaseKeyedObjectPool
* @since Pool 1.0
*/
public interface KeyedObjectPool {
/**
* Obtains an instance from this pool for the specified key
.
*
* Instances returned from this method will have been either newly created with
* {@link KeyedPoolableObjectFactory#makeObject makeObject} or will be a previously idle object and
* have been activated with {@link KeyedPoolableObjectFactory#activateObject activateObject} and
* then validated with {@link KeyedPoolableObjectFactory#validateObject validateObject}.
*
* By contract, clients must return the borrowed object using
* {@link #returnObject returnObject}, {@link #invalidateObject invalidateObject}, or a related method
* as defined in an implementation or sub-interface,
* using a key
that is {@link Object#equals equivalent} to the one used to
* borrow the instance in the first place.
*
* The behaviour of this method when the pool has been exhausted
* is not strictly specified (although it may be specified by implementations).
* Older versions of this method would return null
to indicate exhaustion,
* newer versions are encouraged to throw a {@link NoSuchElementException}.
*
* @param key the key used to obtain the object
* @return an instance from this pool.
* @throws IllegalStateException after {@link #close close} has been called on this pool
* @throws Exception when {@link KeyedPoolableObjectFactory#makeObject makeObject} throws an exception
* @throws NoSuchElementException when the pool is exhausted and cannot or will not return another instance
*/
Object borrowObject(Object key) throws Exception, NoSuchElementException, IllegalStateException;
/**
* Return an instance to the pool.
* By contract, obj
must have been obtained
* using {@link #borrowObject borrowObject}
* or a related method as defined in an implementation
* or sub-interface
* using a key
that is equivalent to the one used to
* borrow the instance in the first place.
*
* @param key the key used to obtain the object
* @param obj a {@link #borrowObject borrowed} instance to be returned.
* @throws Exception
*/
void returnObject(Object key, Object obj) throws Exception;
/**
*
Invalidates an object from the pool.
*
* By contract, obj
must have been obtained
* using {@link #borrowObject borrowObject} or a related method as defined
* in an implementation or sub-interface using a key
that is
* equivalent to the one used to borrow the Object
in the first place.
*
* This method should be used when an object that has been borrowed
* is determined (due to an exception or other problem) to be invalid.
*
* @param key the key used to obtain the object
* @param obj a {@link #borrowObject borrowed} instance to be returned.
* @throws Exception
*/
void invalidateObject(Object key, Object obj) throws Exception;
/**
* Create an object using the {@link KeyedPoolableObjectFactory factory} or other
* implementation dependent mechanism, passivate it, and then place it in the idle object pool.
* addObject
is useful for "pre-loading" a pool with idle objects
* (Optional operation).
*
* @param key the key a new instance should be added to
* @throws Exception when {@link KeyedPoolableObjectFactory#makeObject} fails.
* @throws IllegalStateException after {@link #close} has been called on this pool.
* @throws UnsupportedOperationException when this pool cannot add new idle objects.
*/
void addObject(Object key) throws Exception, IllegalStateException, UnsupportedOperationException;
/**
* Returns the number of instances
* corresponding to the given key
* currently idle in this pool (optional operation).
* Returns a negative value if this information is not available.
*
* @param key the key to query
* @return the number of instances corresponding to the given key
currently idle in this pool or a negative value if unsupported
* @throws UnsupportedOperationException deprecated: when this implementation doesn't support the operation
*/
int getNumIdle(Object key) throws UnsupportedOperationException;
/**
* Returns the number of instances
* currently borrowed from but not yet returned
* to the pool corresponding to the
* given key
(optional operation).
* Returns a negative value if this information is not available.
*
* @param key the key to query
* @return the number of instances corresponding to the given key
currently borrowed in this pool or a negative value if unsupported
* @throws UnsupportedOperationException deprecated: when this implementation doesn't support the operation
*/
int getNumActive(Object key) throws UnsupportedOperationException;
/**
* Returns the total number of instances
* currently idle in this pool (optional operation).
* Returns a negative value if this information is not available.
*
* @return the total number of instances currently idle in this pool or a negative value if unsupported
* @throws UnsupportedOperationException deprecated: when this implementation doesn't support the operation
*/
int getNumIdle() throws UnsupportedOperationException;
/**
* Returns the total number of instances
* current borrowed from this pool but not
* yet returned (optional operation).
* Returns a negative value if this information is not available.
*
* @return the total number of instances currently borrowed from this pool or a negative value if unsupported
* @throws UnsupportedOperationException deprecated: when this implementation doesn't support the operation
*/
int getNumActive() throws UnsupportedOperationException;
/**
* Clears the pool, removing all pooled instances (optional operation).
* Throws {@link UnsupportedOperationException} if the pool cannot be cleared.
*
* @throws UnsupportedOperationException when this implementation doesn't support the operation
*/
void clear() throws Exception, UnsupportedOperationException;
/**
* Clears the specified pool, removing all
* pooled instances corresponding to
* the given key
(optional operation).
* Throws {@link UnsupportedOperationException} if the pool cannot be cleared.
*
* @param key the key to clear
* @throws UnsupportedOperationException when this implementation doesn't support the operation
*/
void clear(Object key) throws Exception, UnsupportedOperationException;
/**
* Close this pool, and free any resources associated with it.
*
* Calling {@link #addObject addObject} or {@link #borrowObject borrowObject} after invoking
* this method on a pool will cause them to throw an {@link IllegalStateException}.
*
*
* @throws Exception
*/
void close() throws Exception;
/**
* Sets the {@link KeyedPoolableObjectFactory factory} the pool uses
* to create new instances (optional operation).
* Trying to change the factory
after a pool has been used will frequently
* throw an {@link UnsupportedOperationException}. It is up to the pool
* implementation to determine when it is acceptable to call this method.
*
* @param factory the {@link KeyedPoolableObjectFactory} used to create new instances.
* @throws IllegalStateException when the factory cannot be set at this time
* @throws UnsupportedOperationException when this implementation doesn't support the operation
* @deprecated to be removed in pool 2.0
*/
void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException;
}