Class PoolUtils.ErodingObjectPool<T>

  • Type Parameters:
    T - type of objects in the pool
    All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, ObjectPool<T>
    Enclosing class:
    PoolUtils

    private static class PoolUtils.ErodingObjectPool<T>
    extends java.lang.Object
    implements ObjectPool<T>
    Decorates an object pool, adding "eroding" behavior. Based on the configured erosion factor, objects returning to the pool may be invalidated instead of being added to idle capacity.
    • Constructor Summary

      Constructors 
      Constructor Description
      ErodingObjectPool​(ObjectPool<T> pool, float factor)
      Creates an ErodingObjectPool wrapping the given pool using the specified erosion factor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addObject()
      Creates an object using the factory or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.
      T borrowObject()
      Obtains an instance from this pool.
      void clear()
      Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).
      void close()
      Closes this pool, and free any resources associated with it.
      int getNumActive()
      Returns the number of instances currently borrowed from this pool.
      int getNumIdle()
      Returns the number of instances currently idle in this pool.
      void invalidateObject​(T obj)
      Invalidates an object from the pool.
      void returnObject​(T obj)
      Returns obj to the pool, unless erosion is triggered, in which case obj is invalidated.
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.datanucleus.store.rdbms.datasource.dbcp2.pool2.ObjectPool

        addObjects
    • Constructor Detail

      • ErodingObjectPool

        public ErodingObjectPool​(ObjectPool<T> pool,
                                 float factor)
        Creates an ErodingObjectPool wrapping the given pool using the specified erosion factor.
        Parameters:
        pool - underlying pool
        factor - erosion factor - determines the frequency of erosion events
        See Also:
        factor
    • Method Detail

      • returnObject

        public void returnObject​(T obj)
        Returns obj to the pool, unless erosion is triggered, in which case obj is invalidated. Erosion is triggered when there are idle instances in the pool and more than the erosion factor-determined time has elapsed since the last returnObject activation.
        Specified by:
        returnObject in interface ObjectPool<T>
        Parameters:
        obj - object to return or invalidate
        See Also:
        factor
      • invalidateObject

        public void invalidateObject​(T obj)
        Invalidates an object from the pool.

        By contract, obj must have been obtained using ObjectPool.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.

        Specified by:
        invalidateObject in interface ObjectPool<T>
        Parameters:
        obj - a borrowed instance to be disposed.
      • addObject

        public void addObject()
                       throws java.lang.Exception,
                              java.lang.IllegalStateException,
                              java.lang.UnsupportedOperationException
        Creates an object using the 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).
        Specified by:
        addObject in interface ObjectPool<T>
        Throws:
        java.lang.Exception - when PooledObjectFactory.makeObject() fails.
        java.lang.IllegalStateException - after ObjectPool.close() has been called on this pool.
        java.lang.UnsupportedOperationException - when this pool cannot add new idle objects.
      • getNumIdle

        public int getNumIdle()
        Returns the number of instances currently idle in this pool. This may be considered an approximation of the number of objects that can be borrowed without creating any new instances. Returns a negative value if this information is not available.
        Specified by:
        getNumIdle in interface ObjectPool<T>
        Returns:
        the number of instances currently idle in this pool.
      • getNumActive

        public int getNumActive()
        Returns the number of instances currently borrowed from this pool. Returns a negative value if this information is not available.
        Specified by:
        getNumActive in interface ObjectPool<T>
        Returns:
        the number of instances currently borrowed from this pool.
      • clear

        public void clear()
                   throws java.lang.Exception,
                          java.lang.UnsupportedOperationException
        Clears any objects sitting idle in the pool, releasing any associated resources (optional operation). Idle objects cleared must be PooledObjectFactory.destroyObject(PooledObject).
        Specified by:
        clear in interface ObjectPool<T>
        Throws:
        java.lang.UnsupportedOperationException - if this implementation does not support the operation
        java.lang.Exception - if the pool cannot be cleared
      • close

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

        Calling ObjectPool.addObject() or ObjectPool.borrowObject() after invoking this method on a pool will cause them to throw an IllegalStateException.

        Implementations should silently fail if not all resources can be freed.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface ObjectPool<T>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object