next up previous contents
Next: Observer Pattern: Update Notification Up: Patterns Previous: Abstract Factory Pattern: Platform   Contents

Object Pool Pattern: Reusable JDBC Connections [58]



$\bullet$ Problem: How to construct time-expensive objects at boot time, and reuse objects during runtime?

$\bullet$ Solution: Object Pool Pattern

$\bullet$ 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.

$\bullet$ Pool is a collection of objects, one as good as another, that can be used, returned, and then reused

$\bullet$ Example: Pool of JDBC connections, each expensive to build, constructed at boot time

$\bullet$ Construct $n$ connections to a database, store in a Vector, place in a Hashtable keyed on database name


\begin{picture}(250,264)(70,540)\thicklines
\put(120,540){\framebox (40,240){...
...0){\makebox(0,0)[lb]{\raisebox{0pt}[0pt][0pt]{\twlrm Connection}}}
\end{picture}

$\bullet$ ConnectionPool is a Singleton, globally accessable

$\bullet$ Main() acquires a Connection, which causes a lookup in the Hashtable, and a dequeue from the Vector

$\bullet$ Using the Connection, main forms and executes an SQL query

$\bullet$ Main() releases Connection, which causes a lookup in Hashtable, and enqueue onto Vector



Object Pool Pattern: Reusable Objects [59]




\begin{picture}(595,620)(20,200)\thicklines
\put(500,610){\framebox (110,30){...
...b]{\raisebox{0pt}[0pt][0pt]{\underline{System.out:OutputStream}}}}
\end{picture}



$\bullet$ ConnectionPool is a Singleton.

$\bullet$ Pool has a Hashtable, indexed by dbName, which returns a Vector.

$\bullet$ Each Vector contains n reusable JDBC Connection objects pre-connected to dbName.


next up previous contents
Next: Observer Pattern: Update Notification Up: Patterns Previous: Abstract Factory Pattern: Platform   Contents
Ted Billard 2006-09-26