You can use instance variables if your action only reads from them. I've done this with a Map variable where I initialize the values in the action constructor and then the execute method just reads from it.
Your action must be assigning the variable to a new object in the execute method to see the behavior you describe.
David
From: "Adolfo Miguelez" <[EMAIL PROTECTED]>
Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Subject: (Un)expected behaviour in Action instance variables?
Date: Mon, 20 Jan 2003 18:08:17 +0000
Hi all,
some of our teams have defined an instance variable in an Action. Wrong policy as said in the online docuemntation:
*******************
http://jakarta.apache.org/struts/userGuide/building_controller.html#action_design_guide
Only Use Local Variables - The most important principle that aids in thread-safe coding is to use only local variables, not instance variables, in your Action class. Local variables are created on a stack that is assigned (by your JVM) to each request thread, so there is no need to worry about sharing them. An Action can be factored into several local methods, so long as all variables needed are passed as method parameters. This assures thread safety, as the JVM handles such variables internally using the call stack which is associated with a single Thread.
*******************
Such a variable is only declared in the class variable declarations an instantiated in perform() executions.
My first impression was that they should refactor the code to move this variable to local as the paragraf above claims. However, we decided to test the execution so we opened two browsers. The surprise was that, despite is true that there was a unique Action, there were a different instance of the variable for each of two browsers. Furthermore, navigating along the app, such a variable was not overridden by the other execution and two instances exist (one for each browser).
Automatically I wondered about the paragraf above, and where was my missuderstanding. I suppose if the variable were either static or instantiated in the Action constructor, the paragraf would be right. But not in our situation, I guess.
Can any of you figure out what am I missuderstanding? Is the paragraf right? Is a different space in memory created for each instantiation, right? How and who knows which one to use to cover each request? The servlet engine? It shouldnt seems, I guess testing out of a web environment the same should happends. How can the Action class maintain a reference for each instance of the variable with a unique declaration (i.e. pointer)?
:-(
I feel an authentic newbie,
regards,
Adolfo.
_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>