"NESTORS Andris (AC-Creation)" wrote:

> The Struts documentation gives some clear instructions on how to set up a
> servlet which will create a DB connection pool (of your choice) on startup.
>
> Out of interest, why use this technique rather than make the connection pool
> a singleton [GoF] which would initialise itself on the first request for a
> connection ?
>

Several reasons.

(1) Personal bias :-).  I'm a long-time code hacker, and I've seen *tons* of
people get themselves in trouble with global variables being modified from all
over the place (remember COMMON blocks in Fortran?).  I much prefer a coding
style that says "if you need a reference to a particular object, you should be
handed that reference, or a way to get it."  (By the way, this is why you see
very very few singleton designs in Struts, and I'd like to get rid of some of
the remaining ones.)

(2) Usage flexibility.  Using attribute lookups (or factory methods in general)
lets you make run-time choices of the particular implementation to be returned.
It's not used in this particular case, but I generally prefer the factory
pattern.

(3) Usage in JSP pages.  Having the connection pool as an application-scope
object enables the creation of custom tags that can use it conveniently.

(4) Consistency with J2EE guidelines.  In a J2EE environment, the recommended
approach is to acquire a reference ot your connection pool via JNDI on each
request.  Using an application scope bean simulates that approach in non-J2EE
environments where JNDI might not be available.


>
> - Nes

Craig


Reply via email to