Cecilia,

(I know nothing about Struts2, but I don't think that's essential to your
query!)

Every one of my Spring DAO beans contains a method along this one:

   public static ThingDAO getInstance() {
       return (ThingDAO) SpringConfig.getSpringContext
().getBean("ThingDAO");
   }

(The String "ThingDAO" corresponds to the name given in the
ApplicationContext.ctx.)

SpringConfig is the following static class:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringConfig {
   private static ApplicationContext ctx;
   private static String[] springXmls = { "/applicationContext.xml" };
   public static ApplicationContext getSpringContext() {
       if (ctx == null)
           ctx = new ClassPathXmlApplicationContext(springXmls);
       return ctx;
   }
}



Now, when the time comes to use a DAO object in an action class, I simply
call the following:

       ThingDAO theDao = ThingDAO.getInstance();
       Thing thing = theDao.findById(thingId);

For the specifics of your query, I would really refer to
springframework.organd their user lists, but as you can see, the DAO
is indeed a singleton kept
withing the SpringContext.  (There is a measurable time difference between
first instantiating and subsequent calls to a given DAO.)

As far as the database connections go, I also use Hibernate with its
connection pool (c3p0), and that must enter into the equation.  But on the
Spring side of things, I am certain there is no question of requesting a
connection before you are fully within a specific "session/transaction" (in
the Hibernate sense, not in the SQL sense), and that connection is released
once the transaction/session is committed.  (I assume that c3p0 keeps it
open in the pool, but that's not Spring's job.)  In the example above, that
would mean that the connection is requested from the pool at some point
within the method findById() and it would be released before the end of the
method...

I hope that helps.  Sorry for the approximate answers!


However, what I want to know is something about how Spring
works.  Does it manage these beans as singletons?  Or every time the
execute() method is called, and I get my dao bean, does it
instantiate a new instance of the dao bean object?  Should I save
this dao bean object in a private member of my ActionSupport class
and reuse it everytime execute() is called?

Right now, I have my ActionSupport class execute method make the
getBean everytime execute is called.

At what point does Spring get a database connection...when  the dao
bean is instantiated, or when the dao bean executes() some jdbc
call.  (I assume this is when it makes a jdbc call, but I just want
to confirm).

If anyone can enlighten me or point me to some good documentation on
this subject, I would appreciate it.

Reply via email to