Problem: How to construct time-expensive objects at boot time, and reuse objects during runtime?
Solution: Object Pool Pattern
Outline: Instead of dynamically constructing time-expensive objects (e.g. network/database
connections), preallocate and reuse the objects as necessary. Application acquires/releases an object
from the pool instead of a constructing the object each time it is needed.
Pool is a collection of objects, one as good as another, that can be used, returned, and then reused
Example: Pool of JDBC connections, each expensive to build, constructed at boot time
Construct
connections to a database, store in a Vector, place in a Hashtable keyed on database name
ConnectionPool is a Singleton, globally accessable
Main() acquires a Connection, which causes a lookup in the Hashtable, and a dequeue from the Vector
Using the Connection, main forms and executes an SQL query
Main() releases Connection, which causes a lookup in Hashtable, and enqueue onto Vector
Object Pool Pattern: Reusable Objects [59]
ConnectionPool is a Singleton.
Pool has a Hashtable, indexed by dbName, which returns a Vector.
Each Vector contains n reusable JDBC Connection objects pre-connected to dbName.