public synchronized T borrowObject()

in src/main/java/org/apache/commons/pool3/impl/SoftReferenceObjectPool.java [174:221]


    public synchronized T borrowObject() throws E {
        assertOpen();
        T obj = null;
        boolean newlyCreated = false;
        PooledSoftReference<T> ref = null;
        while (null == obj) {
            if (idleReferences.isEmpty()) {
                newlyCreated = true;
                obj = factory.makeObject().getObject();
                createCount++;
                // Do not register with the queue
                ref = new PooledSoftReference<>(new SoftReference<>(obj));
                allReferences.add(ref);
            } else {
                ref = idleReferences.pollFirst();
                obj = ref.getObject();
                // Clear the reference so it will not be queued, but replace with a
                // a new, non-registered reference so we can still track this object
                // in allReferences
                ref.getReference().clear();
                ref.setReference(new SoftReference<>(obj));
            }
            if (null != obj) {
                try {
                    factory.activateObject(ref);
                    if (!factory.validateObject(ref)) {
                        throw new Exception("ValidateObject failed");
                    }
                } catch (final Throwable t) {
                    PoolUtils.checkRethrow(t);
                    try {
                        destroy(ref);
                    } catch (final Throwable t2) {
                        PoolUtils.checkRethrow(t2);
                        // Swallowed
                    } finally {
                        obj = null;
                    }
                    if (newlyCreated) {
                        throw new NoSuchElementException("Could not create a validated object, cause: " + t);
                    }
                }
            }
        }
        numActive++;
        ref.allocate();
        return obj;
    }