Howdy all!

I was considering employing a "Master Action Class" from which I could inherit smaller 
action classes in order to make effective use of shared code.  However, I want to 
ensure that the code remains thread-safe.  Here's an example of what I was considering:

public abstract class MainAction extends TilesAction {  

        protected DynaValidatorForm form = null;
        protected ActionErrors errors = null;
        protected ActionMessages messages = null;       
        protected SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy",Locale.US);    
                         
        public abstract ActionForward execute(ActionMapping mapping, ActionForm 
baseForm, HttpServletRequest request, HttpServletResponse response);
        
        protected boolean validLogon(HttpSession session) {
                //check session to ensure that it's not invalidated
        }

        protected void startSubTransaction(HttpSession session) {
                session.setAttribute(Constants.SUB_TRANSACTION_IN_PROGRESS, 
Constants.TRUE);                                                                       
     
        }
        
        protected void endSubTransaction(HttpSession session) {
                session.setAttribute(Constants.SUB_TRANSACTION_IN_PROGRESS, 
Constants.FALSE);   
        }               
}

public class SubAction extends MainAction {

        private static Logger logger = Logger.getLogger(SubAction.class);

        public ActionForward execute(
                ActionMapping mapping,
                ActionForm baseForm,
                HttpServletRequest request,
                HttpServletResponse response) {
                        
                form = (DynaValidatorForm) baseForm;            
                errors = new ActionErrors();                            
                
                if (!validLogon(request.getSession())) {
                        return (mapping.findForward("logon"));                         
                 
                }

                //Check for errors
                if (!errors.isEmpty()) {
                    saveErrors(request, errors);
                return (mapping.findForward("error"));
                }                               
                
                saveToken(request);             
                startSubTransaction(request.getSession());              
                return (mapping.findForward("success"));
        }
}

What with the form, errors & messages variables being defined at class level in the 
parent class, could I run into some issues with instance variables being overwritten 
with different requests?  Should I move them into the individual execute() methods of 
the subclasses of MainAction?

Thanks,

Brent


-----Original Message-----
From: e-denton Java Programmer [mailto:[EMAIL PROTECTED]
Sent: Monday, December 22, 2003 1:39 PM
To: Struts Users Mailing List
Subject: Re: Example of a non-threadsafe Action?


Hi! Don't store any request related items in static or instance variables.

----- Original Message ----- 
From: "David Erickson" <[EMAIL PROTECTED]>
To: "Struts Mailing List" <[EMAIL PROTECTED]>
Sent: Monday, December 22, 2003 1:02 PM
Subject: Example of a non-threadsafe Action?


> Hey I have been reading a lot about threading lately from the JLS and
> otherwise.. but my question is what would be an example of a
non-threadsafe
> action?  Struts manual said that only one instance of an action exists in
> the JVM.. and when I run an action each thread creates its own versions of
> all the variables within the action correct?  So assuming I'm not
accessing
> some outside 'global' variable, everything is inherintly threadsafe right?
>
> If anyone can give me an example of what 'not' to do that would help
> tremendously.
> thanks,
> David
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

____________________________________________________________
This message sent using iMail from I-Land Internet Services.
http://www.iland.net 


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

Reply via email to