# Implementing a Custom Application Module Pool

You can create your own custom implementations of application module pooling by extending the `ApplicationPoolImpl` class and overriding the methods you need. For example, you can define a class, `CustomPool`, that ensures that no more than three instances are checked out of the pool at any time. In this case, the definition of the `checkout` method is overridden to check that no more than three instances are checked out. If more than three are checked out, an exception is thrown.

     1  /**
     2   **  This is a simple application pool class that allows checkout
     3   **  of only three instances.
     4   **/
     5  class CustomPool extends ApplicationPoolImpl
     6  {
     7  public CustomPool()
     8  {
     9  }

    10  /**
    11   * Ensures that only three instances are
    12   * checked out of the pool. Otherwise, a
    13   * RuntimeException is thrown.
    14   *
    15   * @return oracle.jbo.ApplicationModule
    16   */
    17  public synchronized ApplicationModule createNewInstance()
    18  {
    19    // if the instance count > 3 throw an exception
    20    int nCount = getInstanceCount();
    21    if(nCount > 3)
    22    {
    23      throw new RuntimeException("You have exceeded the number of
                 instances for this pool");
    24    }
    25    return super.checkout();
    26  }

    27 }

Line 5: Create a `CustomPool` class that extends `ApplicationPoolImpl`.

Lines 17-23: Override the `checkout` method to test whether more than three instances are checked out. If more than three are checked out, throw a `RuntimeException`.

### Using the custom application module pool Implementation

You can then use the `CustomPool` class in your programs.

     1 public class PoolTester extends Object {

     2   /**
     3    * Constructor
     4    */
     5   public PoolTester() {
     6   }
     7   public void doTest() throws Exception
     8   {
     9     Hashtable info = new Hashtable();

    10    info.put(JboContext.INITIAL_CONTEXT_FACTORY, JboContext.JBO_CONTEXT_FACTORY);
    11    info.put(JboContext.DEPLOY_PLATFORM, "LOCAL");

    12    // create a pool with a custom application pool class
    13    PoolMgr.getInstance().createPool("CustomTestAMPool",
           "oracle.jbo.common.ampool.CustomPool" , "package2.Package2Module" ,
           "jdbc:oracle:thin:scott/tiger@localhost:1521:orcl", info);
    14    pool = PoolMgr.getInstance().getPool("CustomTestAMPool");

    15    try
    16    {
    17      instance = pool.checkout();
    18      instance = pool.checkout();
    19      instance = pool.checkout();

    20      instance = pool.checkout();
    21      instance = pool.checkout();
    22    }
    23    catch(Exception ex)
    24     {
    25       ex.printStackTrace();
    26     }
    27  }

Lines 9-11: Define the environment settings. This can be done directly (as is demonstrated here) or from a property file. In this example, the hashtable contains the properties to set up the environment to connect in `LOCAL` mode. For more information about the code you need to supply to set up the environment for `LOCAL` mode and the other platforms, see Setting up the Environment.

Lines 12-14: Use the `CustomPool` class to create the pool `CustomTestAMPool`. Since `PoolMgr` is a singleton object, use `PoolMgr.getInstance` to get it. Use `createPool` to create the pool. Notice that in this case, `createPool` requires the name of the pool, the name of the class that overrides the default implementation of application module pooling, the name of the package that contains the application module, the connect string to the database, and the name of the hashtable.

Lines 15-25: Set up a `try`-`catch` loop to start checking out instances and to catch exceptions. The `pool.checkout()` in line 20 should cause an exception to be thrown.

Related topics
Pooling Application Modules
About Application Module Pooling
About Application Module Pooling Classes
About Configuration Properties for a JSP Project