[prev in list] [next in list] [prev in thread] [next in thread] 

List:       struts-user
Subject:    Re: newbie question...struts2, spring and jdbc
From:       "Joe Germuska" <joe () germuska ! com>
Date:       2007-01-31 22:25:40
Message-ID: 242960fe0701311425s22ef1215ofadeac3be221f90d () mail ! gmail ! com
[Download RAW message or body]


On 1/31/07, Cecilia Castillo <castillo23@llnl.gov> wrote:
>
> I am new to Struts, Struts2, and Spring.  (And maybe know too much about
> jdbc.)
>
> I have written my application with Struts2 and am only using Spring
> to manage my database connections.  I have defined my oracle database
> as a JNDI resource in my Tomcat server.xml file.
>
> I have defined a dataSource bean in my  applicationContext.xml for my
> database JNDI resource.
>
> I have written some DAO classes which are spring beans defined in my
> applicationContext.xml.  These all have a dataSource property
> pointing to my database.  These DAO classes have methods which
> execute various db stored procedures or select statements.
>
> Now, I have some strut2 ActionSupport  subclasses which do something
> like this in my execute() method
>                 ThingDao thingDao= (ThingDao )getBean("ThingDao");
>
>
> All is well, all is working.
>
> However, what I want to know is something about how Spring
> works.  Does it manage these beans as singletons?


In Spring 2, bean lifecycle is controlled by the "scope" attribute of the
<bean> element.

See
http://www.springframework.org/docs/reference/beans.html#beans-factory-scopesfor
more info.

Also, the scope defaults to "singleton", which is counter to the original
model for Struts2 Actions, where each action is generally expected to be
instantiated once per request.

 For your DAO bean, etc, of course you would choose for yourself whether to
set to a "singleton" scope or a "prototype" scope.

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?


This suggests that your model is also counter to the Struts2 model.  If you
are writing Action classes with properties that are populated based on
request parameters, you will be subject to concurrency problems (race
conditions) if you have singleton actions.  I'd recommend against it.
Actions are supposed to be lightweight.

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).


This I can't answer directly, but remember that Spring is open source.  You
could check the code.

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


Spring's own documentation (referenced above) is thorough and pretty clear.
There are also a number of good Spring books.

Hope this helps.

Joe

-- 
Joe Germuska
Joe@Germuska.com * http://blog.germuska.com

"The truth is that we learned from Joćo forever to be out of tune."
-- Caetano Veloso


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic