Class PoolUtils.SynchronizedPoolableObjectFactory<T>

  • All Implemented Interfaces:
    PoolableObjectFactory<T>
    Enclosing class:
    PoolUtils

    private static class PoolUtils.SynchronizedPoolableObjectFactory<T>
    extends java.lang.Object
    implements PoolableObjectFactory<T>
    A fully synchronized PoolableObjectFactory that wraps a PoolableObjectFactory and synchronizes access to the wrapped factory methods.

    Note: This should not be used on pool implementations that already provide proper synchronization such as the pools provided in the Commons Pool library.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void activateObject​(T obj)
      Reinitialize an instance to be returned by the pool.
      void destroyObject​(T obj)
      Destroys an instance no longer needed by the pool.
      T makeObject()
      Creates an instance that can be served by the pool.
      void passivateObject​(T obj)
      Uninitialize an instance to be returned to the idle object pool.
      java.lang.String toString()
      boolean validateObject​(T obj)
      Ensures that the instance is safe to be returned by the pool.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • lock

        private final java.lang.Object lock
        Synchronization lock
    • Constructor Detail

      • SynchronizedPoolableObjectFactory

        SynchronizedPoolableObjectFactory​(PoolableObjectFactory<T> factory)
                                   throws java.lang.IllegalArgumentException
        Create a SynchronizedPoolableObjectFactory wrapping the given factory.
        Parameters:
        factory - underlying factory to wrap
        Throws:
        java.lang.IllegalArgumentException - if the factory is null
    • Method Detail

      • makeObject

        public T makeObject()
                     throws java.lang.Exception
        Creates an instance that can be served by the pool. Instances returned from this method should be in the same state as if they had been activated. They will not be activated before being served by the pool.
        Specified by:
        makeObject in interface PoolableObjectFactory<T>
        Returns:
        an instance that can be served by the pool.
        Throws:
        java.lang.Exception - if there is a problem creating a new instance, this will be propagated to the code requesting an object.
      • destroyObject

        public void destroyObject​(T obj)
                           throws java.lang.Exception
        Destroys an instance no longer needed by the pool.

        It is important for implementations of this method to be aware that there is no guarantee about what state obj will be in and the implementation should be prepared to handle unexpected errors.

        Also, an implementation must take in to consideration that instances lost to the garbage collector may never be destroyed.

        Specified by:
        destroyObject in interface PoolableObjectFactory<T>
        Parameters:
        obj - the instance to be destroyed
        Throws:
        java.lang.Exception - should be avoided as it may be swallowed by the pool implementation.
        See Also:
        PoolableObjectFactory.validateObject(T), ObjectPool.invalidateObject(T)
      • validateObject

        public boolean validateObject​(T obj)
        Ensures that the instance is safe to be returned by the pool. Returns false if obj should be destroyed.
        Specified by:
        validateObject in interface PoolableObjectFactory<T>
        Parameters:
        obj - the instance to be validated
        Returns:
        false if obj is not valid and should be dropped from the pool, true otherwise.
      • activateObject

        public void activateObject​(T obj)
                            throws java.lang.Exception
        Reinitialize an instance to be returned by the pool.
        Specified by:
        activateObject in interface PoolableObjectFactory<T>
        Parameters:
        obj - the instance to be activated
        Throws:
        java.lang.Exception - if there is a problem activating obj, this exception may be swallowed by the pool.
        See Also:
        PoolableObjectFactory.destroyObject(T)
      • passivateObject

        public void passivateObject​(T obj)
                             throws java.lang.Exception
        Uninitialize an instance to be returned to the idle object pool.
        Specified by:
        passivateObject in interface PoolableObjectFactory<T>
        Parameters:
        obj - the instance to be passivated
        Throws:
        java.lang.Exception - if there is a problem passivating obj, this exception may be swallowed by the pool.
        See Also:
        PoolableObjectFactory.destroyObject(T)
      • toString

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