struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Lindholm <>
Subject Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Date Thu, 07 Jan 2010 20:16:22 GMT
> Instantiating the DAO in the execute() method of each of my action's
> however, seems a little inefficient.  Does Struts provide any way to
> instantiate the object once and then make it available for the lifetime of
> the container (in a way that my Actions can access it)?

So want your DAO's to be singletons?
You would have to make them Thread-safe (or synchronize and kill
performance), beware that database connections are not thread-safe.
Doesn't really sound like a good plan to me.

If you just have one DAO of each type (singletons) then you need to
ensure they are thread-safe and you need to either handle the
acquire/release of database connections within each method or pass in
the connection.  Neither is a great design, the first make you
duplicate fragile difficult to test code in every DAO the second
exposes database connections in the DAO methods.  It's hard to write
DAO's as singletons and get them right, much easier to use a new one
each time as if they have no state then instantiating them is cheap
(where writing thread safe code is hard, hard to write, hard to test).

The preferred method is use a DI facility (like Guice or Spring) and
instantiate a new DAO each time and inject the database connection
into it and inject the DAO into the Struts action.  This make it easy
to write, easy to test and you don't have to expose database
connections and handling logic to the actions.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message