H.Object Pool

  1. Summary

    1. Reuse and share objects that are expensive to create.

    2. Whenever there are several clients who needs the same stateless resource which is expensive to create.

    3. Pools are generally singleton and created reusable object like database connection or HTTP Connection.

  2. When to Use

      1. When resources are limited and you want to use in controlled manner

  3. Overview Tutorial

  1. Class Diagram

      1. Client holds a class called Pool that contains object of client interest.

      2. Objects are marked as Acquired or Releases.

      3. Once Client request an object pool return any available object marking them as Acquired

      4. If no resources available then pool wait for object availability.

      5. In order to wait improve performance pool must recycled the object that are not in use, this can be implemented by using timeout with resource.

  1. Example

      1. Database connection sharing or thread pools are good example of it.

      2. Network Connection Manager

  2. Specific Problems and Consideration

    1. Handling unused resources

        1. When the Object Pool pattern is used the objects should be marked as available(released) by the client after they are used, so the pool will be aware about this.

        2. This is the main drawback because the client should do this and it's a common situation when database connection is not released after they are used.

        3. To overcome this problem cleanup mechanism can be implemented to release resources if they are not used for a period of time.

    2. Handling Resources Creation Failure

        1. Creating the resources might fail and this case should be treated carefully.

        2. When there is no available resource (because the number is limited or creating a new one failed) the client should be notified about it.

    1. Synchronization Issue

        1. Although the object pool is handling the object instantiation it`s main purpose is to provide a way for the clients to reuse the objects like they are new objects, without being shared and reused.

        2. This present synchronization as challenge because object access need to be mutually exclusive.