Laurie:
        Thanks for your response.
        You are right big mistake ! 
        Even if Java has pass by value, if you want to make it work like 
pass by reference you need to have Valid object  ( one which is 
instatiated).
        Also I wanted initialize method to do work of accessing Session 
and other initialization function.
        I figured out I needed Map to put all required objects in it and 
then get Object from map from various different ActionForward method in my 
                DispatchAction case.

        Is this best way to achieve thread safety ?
        Thanks.
Digant





Laurie Harper <[EMAIL PROTECTED]>
Sent by: news <[EMAIL PROTECTED]>
02/16/2006 01:36 PM
Please respond to "Struts Users Mailing List"

 
        To:     user@struts.apache.org
        cc: 
        Subject:        Re: Thread safety in struts and better practice 
question !


Java just doesn't work like that. See inline:

[EMAIL PROTECTED] wrote:
> My understanding for making Struts action thread safe you shouldn't have 

> instance variable in any action class. Correct ?
> ( it should be OK if you have read only variable ). But to make app for 
> flexible, so in future you can change value of variable and pass it 
around
> helper functions, I made variable local. as shown in  the following 
code.
> 
> Two questions .
> 1.  I am getting Null pointer exception in setUpForInsertOrUpdate even 
> after calling initialize ! How come ?
> 
> 
> 2.   This service objects are used by many dispatch actions methods for 
my 
> app, it contains data from DAO object.
>        What is the best practice so I don't keep declaring instance 
> variable in all DispatchAction methods like ( update/delete) and so on.
> 
> 
> Code is like this.
> 
> 
> public class SectorAction extends DispatchAction {
> 
> 
>  public void  initialize(HttpServletRequest request,SectorService 
> sectService,SectorOwnerService sectOwnerService) throws Exception
>     {
> 
>         sectService = (SectorService) 
> request.getSession().getAttribute("SectorService");
>                 sectOwnerService = (SectorOwnerService) 
> request.getSession().getAttribute("SectorOwnerService");
> 
>     }

This just updates the local values of sectService / sectOwnerService 
within initialize(). That has no effect on the formal parameters you 
pass in:


>     public ActionForward setUpForInsertOrUpdate(ActionMapping mapping, 
> ActionForm form, HttpServletRequest request, HttpServletResponse 
> response) throws Exception {
>         SectorService sectService = null;
>                 SectorOwnerService sectOwnerService = null;
> 
> 
>                 initialize(request,sectService,sectOwnerService);

The sectService/sectOwnerService in setUpForInsertOrUpdate() aren't 
modifed by this call. All you're doing is passing null references to 
initialize().

>                 SectorForm sectForm = (SectorForm)form;
> 
>                 WebSector sector = 
> sectService.getSector(sectForm.getSectorId());
>                 BeanUtils.copyProperties(sectForm, sector);
> 
>         return mapping.findForward(Constants.SUCCESS);
>      }
>       public ActionForward delete(ActionMapping mapping, ActionForm 
form, 
> HttpServletRequest request, HttpServletResponse response) throws 
> Exception {
>         SectorService sectService = new SectorDaoService();
>                 SectorOwnerService sectOwnerService = new 
> SectorOwnerDaoService();
> 
>         initialize(request,sectService,sectOwnerService); 
> 
> 
>         SectorForm sectForm = (SectorForm)form;
> 
>         sectService.deleteSector(sectForm.getSectorId());
>         populateSectors(request,sectService,sectOwnerService);
>         return mapping.findForward(Constants.SUCCESS);
>     }
> 
> }
> 
> in my action forward I can see initialize has valid sectService and 
> sectOwnerService objects but once initialze comes back to 
> setUpForInsertOrUpdate method this service objects are null and I am 
> getting NullPointerException.
> 
> 
> I thought even if calls in java are passed by value, it should 
> some how after I call initialize and get my two service objects, i

Nope. All you're doing is passing (null) references in to a function 
which then overwrites its local (null) copies. You need to pass a valid 
(non-null) reference to a container object and store the objects you 
want to return into that, or simply return an object from initialize() 
that you can pull what you need back out of.

You might want to read up on the basics of how function invocation and 
paramter passing work in Java.

L.

> 
> 
> Digant 
> 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
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




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

Reply via email to