/* * 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.pool2.impl; import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.SoftReference; import java.util.ArrayList; import java.util.Iterator; import java.util.NoSuchElementException; import org.apache.tomcat.dbcp.pool2.BaseObjectPool; import org.apache.tomcat.dbcp.pool2.PoolUtils; import org.apache.tomcat.dbcp.pool2.PooledObjectFactory; /** * A {@link java.lang.ref.SoftReference SoftReference} based * {@link org.apache.tomcat.dbcp.pool2.ObjectPool}. *
* This class is intended to be thread-safe. *
* * @param_pool. This is used to help {@link #getNumIdle()} be more
* accurate with minimal performance overhead.
*/
private final ReferenceQueueSoftReferenceObjectPool with the specified factory.
*
* @param factory object factory to use.
*/
public SoftReferenceObjectPool(final PooledObjectFactory
* All instances are {@link PooledObjectFactory#activateObject(
* org.apache.tomcat.dbcp.pool2.PooledObject) activated}
* and {@link PooledObjectFactory#validateObject(
* org.apache.tomcat.dbcp.pool2.PooledObject)
* validated} before being returned by this method. If validation fails or
* an exception occurs activating or validating an idle instance, the
* failing instance is {@link PooledObjectFactory#destroyObject(
* org.apache.tomcat.dbcp.pool2.PooledObject)
* destroyed} and another instance is retrieved from the pool, validated and
* activated. This process continues until either the pool is empty or an
* instance passes validation. If the pool is empty on activation or it does
* not contain any valid instances, the factory's
* Before being added to the pool, the newly created instance is
* {@link PooledObjectFactory#validateObject(
* org.apache.tomcat.dbcp.pool2.PooledObject) validated} and
* {@link PooledObjectFactory#passivateObject(
* org.apache.tomcat.dbcp.pool2.PooledObject) passivated}. If
* validation fails, the new instance is
* {@link PooledObjectFactory#destroyObject(
* org.apache.tomcat.dbcp.pool2.PooledObject) destroyed}. Exceptions
* generated by the factory
* Calling {@link #addObject} or {@link #borrowObject} after invoking this
* method on a pool will cause them to throw an
* {@link IllegalStateException}.
*/
@Override
public void close() {
super.close();
clear();
}
/**
* Returns the {@link PooledObjectFactory} used by this pool to create and
* manage object instances.
*
* @return the factory
*/
public synchronized PooledObjectFactorymakeObject
* method is used to create a new instance. If the created instance either
* raises an exception on activation or fails validation,
* NoSuchElementException is thrown. Exceptions thrown by
* MakeObject are propagated to the caller; but other than
* ThreadDeath or VirtualMachineError, exceptions
* generated by activation, validation or destroy methods are swallowed
* silently.
*
* @throws NoSuchElementException
* if a valid object cannot be provided
* @throws IllegalStateException
* if invoked on a {@link #close() closed} pool
* @throws Exception
* if an exception occurs creating a new instance
* @return a valid, activated object instance
*/
@SuppressWarnings("null") // ref cannot be null
@Override
public synchronized T borrowObject() throws Exception {
assertOpen();
T obj = null;
boolean newlyCreated = false;
PooledSoftReference
*
* Exceptions passivating or destroying instances are silently swallowed.
* Exceptions validating instances are propagated to the client.
*
* @param obj
* instance to return to the pool
* @throws IllegalArgumentException
* if obj is not currently part of this pool
*/
@Override
public synchronized void returnObject(final T obj) throws Exception {
boolean success = !isClosed();
final PooledSoftReferencemakeObject or
* passivate are propagated to the caller. Exceptions
* destroying instances are silently swallowed.
*
* @throws IllegalStateException
* if invoked on a {@link #close() closed} pool
* @throws Exception
* when the {@link #getFactory() factory} has a problem creating
* or passivating an object.
*/
@Override
public synchronized void addObject() throws Exception {
assertOpen();
if (factory == null) {
throw new IllegalStateException(
"Cannot add objects without a factory.");
}
final T obj = factory.makeObject().getObject();
createCount++;
// Create and register with the queue
final PooledSoftReference