Why and when to perform this task
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. These object pools are not meant to be used for pooling JDBC connections or Java Message Service (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.
Steps for this task
Result
After you have completed these steps, applications can find the object pool manager by doing a JNDI lookup using the specified JNDI name.Example
InitialContext ic = new InitialContext(); ObjectPoolManager opm = (ObjectPoolManager)ic.lookup("java:comp/env/pool");
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 }
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 that the object cannot be reused.