To answer your question, in general, its a very bad idea to
synchronize action methods as that will bring your webapp to a
screeching halt because Struts only creates a single instance of a
given action class. What you will be doing is essentially handling a
single request at a time, very bad.
Now, the reason why you want to synchronize access is based on a bad
design idea. You DO NOT want to have an instance to a database
connection shared between different requests because of the
concurrency issues you refer to. You don't even want to share a
connection within a session for a couple of reasons: you don't want a
connection open across different requests, and even if holding
connections open for a long time isn't bad enough, you can have
multiple client web pages that are part of the same session hitting
the same connection at the same time, which will certainly break
something, and probaly not until your web app is in production :-)
You need to do some research on how to use a connection pool, and then
have your DAOs either get a connection from the connection pool or
take a connection as an argument which will be supplied by the action.
I prefer the latter, that way you can chain operations on the database
in an action without the overhead of each dao call getting and then
freeing a connection from the pool. Also, transactions across DAOs are
much easier with the latter solutions too.
I don't have any links handy, but there's probably at least a
half-dozen pages about this on the Struts wiki that you can find at
http://struts.apache.org/
HTH,
-ed
On 3/21/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Is it a good idea to synchronize DispathAction to make sure all requests
> to DB and results which come back don't overstep each other ?
> Pls help.
>
> Following is some detail of what am I doing in my application.
>
> I have few operation I want to synchronize.
> In my app following steps I am taking
> 1. calling dispatch Action getInstrumentsFromQ as under, Should
> this be synchronized ?
>
> public ActionForward getInstrumentsFromQ(ActionMapping
> mapping, ActionForm form, HttpServletRequest request, HttpServletResponse
> response) throws Exception {
> ..
> ..
> }
> 2. In Dao service I have couple of lists which are stored as Class
> variable, where I store one I received from DB.
> public class InstrumentDiceQdao extends BaseDao implements
> InstrumentDao{
>
> private List instruments =(List )
> Collections.synchronizedList(new ArrayList());
> private List instrumentDbItems = (List)
> Collections.synchronizedList(new ArrayList());
>
>
>
> public synchronized List
> getAllInstrumentsFromQ(Long sectorId) throws DiceWebException {
> ...
> ...
> }
> }
> 3. I am putting service object which has results of all DB query
> in Session, so I can access from various places in web.
>
> InstrumentService instService = new
> InstrumentDaoService(ds) ;
>
> try {
> synchronized(instService)
> {
> List instruments =
> instService.getAllinstrumentsFromQ(sectorId);
> session.setAttribute("InstrumentService",instService);
> }
> }
>
>
> This communication is for informational purposes only. It is not intended
> as an offer or solicitation for the purchase or sale of any financial
> instrument or as an official confirmation of any transaction. All market
> prices,
> data and other information are not warranted as to completeness or accuracy
> and
> are subject to change without notice. Any comments or statements made herein
> do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
> and affiliates.
>
--
"The greatest tyrannies are always perpetrated in the name of the
noblest causes." Thomas Paine
"Those who would give up essential Liberty, to purchase a little
temporary Safety, deserve neither Liberty nor Safety" - Benjamin
Franklin
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]