# Implementing Pooling in Application Module Code

One of the tasks you must perform to enable application module pooling is to set up the environment information needed to connect to the application module. The information you supply will depend on the platform on which the application module resides. To find out more about the code you need to supply to set up the environment for the various platforms, see Setting up the Environment.

### An example implementation of a stateful application

The `PoolTester` class provides a sample implementation and use of application module pooling in `LOCAL` mode. This example uses the default implementation of application module pooling.

     1 public class PoolTester extends Object {

     2 /**
     3    * Constructor
     4  */
     5 public PoolTester() {
     6 }

     7 public void doTest() throws Exception
     8 {

     9   Hashtable env = new Hashtable(10);

    10   env.put(JboContext.INITIAL_CONTEXT_FACTORY, JboContext.JBO_CONTEXT_FACTORY);
    11   env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_LOCAL);

    12   PoolMgr.getInstance().createPool("TestAMPool", "Package1.MyAppMod",
              "jdbc:oracle:thin:scott/tiger@localhost:1521:orcl", env);

    13   PoolMgr.getInstance().createPool("SecondAMPool", "Package2.MyAppMod",
              "jdbc:oracle:thin:scott/tiger@localhost:1521:orcl", env);

    14   ApplicationPool pool = PoolMgr.getInstance().getPool("TestAMPool");

    15   ApplicationModule instance = pool.checkout();

    16   ApplicationModule instance2 = pool.checkout();

    17   ApplicationModule instance3 = pool.checkout();

    18   ApplicationPool pool2 = PoolMgr.getInstance().getPool("SecondAMPool");

    19   ApplicationModule instance22 = pool2.checkout();

    20   ApplicationModule instance23 = pool2.checkout();

    21   String sessionId = pool2.checkinWithSessionState(instance22);

    22   instance22 = pool2.checkout(sessionId);

    23   PoolMgr.getInstance().removePool("TestAMPool");

    24 }

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.

Line 12: Create the pool `TestAMPool`. Since `PoolMgr` is a singleton object, use `PoolMgr.getInstance` to get it. Use `createPool` to create the pool. Notice that `createPool` requires the name of the pool, the name of the package that contains the application module, the connect string to the database, and `env`, the hashtable. The `createPool` method is overloaded—an alternative version takes an additional parameter that lets you override the default implementation of application module pooling and specify your own.

Line 13: Create a second pool, `SecondAMPool` that connects with the same environment variables, but to an application module in a different package.

Line 14: `PoolMgr.getInstance().getPool("TestAMPool")` gets a handle to the pool to work with it.

Lines 15-17: Checkout three instances. Note that the classes `ApplicationPoolImpl` and `PoolMgr` contain other functions that you can use to traverse the pool content. The `ampool` package also contains a `PoolAdministrator` Web Bean that you can use to dump the contents of the pool.

Lines 18-20: Get a handle to the second pool, check out two instances.

Lines 21-22: Check in an application module instance in a stateful fashion, stroring the session ID in a local String. Check out an application module with the same state that was checked in.

Line 23: Use `removePool` to remove the application module pool. This function calls `remove()` on all of the application module instances, including ones that are currently checked out and in use, and causes them to disconnect from the database and remove all of their view objects.

### An example implementation of a stateless application

The `PoolTester` class provides a sample implementation and use of application module pooling in `LOCAL` mode. This example uses the default implementation of application module pooling.

     1 public class PoolTester extends Object {

     2 /**
     3    * Constructor
     4  */
     5 public PoolTester() {
     6 }

     7 public void doTest() throws Exception
     8 {

     9   Hashtable env = new Hashtable(10);

    10   env.put(JboContext.INITIAL_CONTEXT_FACTORY, JboContext.JBO_CONTEXT_FACTORY);
    11   env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_LOCAL);

    12   PoolMgr.getInstance().createPool("TestAMPool", "Package1.MyAppMod",
              "jdbc:oracle:thin:scott/tiger@localhost:1521:orcl", env);

    13   PoolMgr.getInstance().createPool("SecondAMPool", "Package2.MyAppMod",
              "jdbc:oracle:thin:scott/tiger@localhost:1521:orcl", env);

    14   ApplicationPool pool = PoolMgr.getInstance().getPool("TestAMPool");

    15   ApplicationModule instance = pool.checkout();

    16   ApplicationModule instance2 = pool.checkout();

    17   ApplicationModule instance3 = pool.checkout();

    18   ApplicationPool pool2 = PoolMgr.getInstance().getPool("SecondAMPool");

    19   ApplicationModule instance22 = pool2.checkout();

    20   ApplicationModule instance23 = pool2.checkout();

    21   pool2.checkin(instance22);

    22   PoolMgr.getInstance().removePool("TestAMPool");

    23 }

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.

Line 12: Create the pool `TestAMPool`. Since `PoolMgr` is a singleton object, use `PoolMgr.getInstance` to get it. Use `createPool` to create the pool. Notice that `createPool` requires the name of the pool, the name of the package that contains the application module, the connect string to the database, and `env`, the hashtable. The `createPool` method is overloaded—an alternative version takes an additional parameter that lets you override the default implementation of application module pooling and specify your own.

Line 13: Create a second pool, `SecondAMPool` that connects with the same environment variables, but to an application module in a different package.

Line 14: `PoolMgr.getInstance().getPool("TestAMPool")` gets a handle to the pool to work with it.

Lines 15-17: Checkout three instances. Note that the classes `ApplicationPoolImpl` and `PoolMgr` contain other functions that you can use to traverse the pool content. The `ampool` package also contains a `PoolAdministrator` Web Bean that you can use to dump the contents of the pool.

Lines 18-21: Get a handle to the second pool, check out two instances, then check in one.

Line 22: Use `removePool` to remove the application module pool. This function calls `remove()` on all of the application module instances, including ones that are currently checked out and in use, and causes them to disconnect from the database and remove all of their view objects.

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