WAS v8.5 > Develop applications > Develop Object poolsUse object pools
An object pool helps an application avoid creating new Java objects repeatedly. Most objects can be created once, used and then reused. An object pool supports the pooling of objects waiting to be reused. Object pools are not meant to be used for pooling JDBC connections or JMS connections and sessions. WebSphere Application Server provides specialized mechanisms for dealing with those types of objects. These object pools are intended for pooling application-defined objects or basic Developer Kit types.
To use an object pool, the product administrator must define an object pool manager using the dmgr console. Multiple object pool managers can be created in an Application Server cell.
The Object pool manager service is only supported from within the EJB container or Web container. Looking up and using a configured object pool manager from a Java 2 Platform Enterprise Edition (J2EE) application client container is not supported.
- Start the dmgr console.
- Click Resources > Object pool managers.
- Specify a Scope value and click New.
- Specify the required properties for work manager settings.
- Scope
- The scope of the configured resource. This value indicates the location for the configuration file.
- Name
- The name of the object pool manager. This name can be up to 30 ASCII characters long.
- JNDI Name
- The JNDI name for the pool manager.
- [Optional] Specify a Description and a Category for the object pool manager.
Results
After we have completed these steps, applications can find the object pool manager by doing a JNDI lookup using the specified JNDI name.
Example
The following code illustrates how an application can find an object pool manager object:
InitialContext ic = new InitialContext(); ObjectPoolManager opm = (ObjectPoolManager)ic.lookup("java:comp/env/pool");When the application has an ObjectPoolManager, it can cache an object pool for classes of the types it wants to use. The following is an example:
ObjectPool arrayListPool = null; ObjectPool vectorPool = null; try { arrayListPool = opm.getPool(ArrayList.class); vectorPool = opm.getPool(Vector.class);} catch(InstantiationException e) { // problem creating pool} catch(IllegalAccessException e) { // problem creating pool}When the application has the pools, the application can use them as in the following example:
ArrayList list = null; try { list = (ArrayList)arrayListPool.getObject(); list.clear(); // just in case for(int i = 0; i < 10; ++i) { list.add("" + i); } // do what ever we need with the ArrayList} finally { if(list != null) arrayListPool.returnObject(list);}This example presents the basic pattern for using object pooling. If the application does not return the object, then the only adverse effect is the object cannot be reused.
Subtopics
- Object pool managers
Object pool managers control the reuse of application objects and Developer Kit objects, such as Vectors and HashMaps.- Object pool managers page
An object pool manages a pool of arbitrary objects and helps applications avoid creating new Java objects repeatedly. Most objects can be created once, used and then reused. An object pool supports the pooling of objects waiting to be reused. These object pools are not meant to be used for pooling Java Database Connectivity connections or JMS connections and sessions. WAS provides specialized mechanisms for dealing with those types of objects. These object pools are intended for pooling application-defined objects or basic Developer Kit types.- Object pool service settings
Use this page to enable or disable the object pool service, which manages object pool resources used by the server.- Object pools: Resources for learning
This topic provides links to find relevant supplemental information about object pools.- MBeans for object pool managers and object pools
Legacy MBean names for object pool managers and object pools are deprecated. The legacy names are based on the object pool manager name (which is not required to be unique) rather than the object pool manager JNDI name.- Object pool managers
Object pool managers control the reuse of application objects and Developer Kit objects, such as Vectors and HashMaps.- Object pool managers page
An object pool manages a pool of arbitrary objects and helps applications avoid creating new Java objects repeatedly. Most objects can be created once, used and then reused. An object pool supports the pooling of objects waiting to be reused. These object pools are not meant to be used for pooling Java Database Connectivity connections or JMS connections and sessions. WAS provides specialized mechanisms for dealing with those types of objects. These object pools are intended for pooling application-defined objects or basic Developer Kit types.- Object pool service settings
Use this page to enable or disable the object pool service, which manages object pool resources used by the server.- Object pools: Resources for learning
This topic provides links to find relevant supplemental information about object pools.- MBeans for object pool managers and object pools
Legacy MBean names for object pool managers and object pools are deprecated. The legacy names are based on the object pool manager name (which is not required to be unique) rather than the object pool manager JNDI name.