You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by di...@jpmorgan.com on 2006/02/16 19:08:29 UTC
Thread safety in struts and better practice question !
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");
}
public ActionForward setUpForInsertOrUpdate(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse
response) throws Exception {
SectorService sectService = null;
SectorOwnerService sectOwnerService = null;
initialize(request,sectService,sectOwnerService);
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
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
Re: Thread safety in struts and better practice question !
Posted by Laurie Harper <la...@holoweb.net>.
Java just doesn't work like that. See inline:
digant.k.joshi@jpmorgan.com 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org