Can we use instance variables in Stateless session beans? If yes, Why and How? If the answer is no , explain why and how?
Yes you can.
You can save instance variables with in session beans.
Issue is that you cannot gaurrentee that you will call the same session bean with each method call, as the pool can return any session bean for your method call.
So you cant save inastance variable as something like a userId and expect proper behaviour, but something that is costly to create each time and doesnt matter who calls it like an sql.Connection is a good idea to save.
You can, but you're running into a risk of keeping the instance values created by the bean and if the next client tries to use your bean, those values will be exposed, thus creating unnecessary confusion.
Simply decalare the variables as you would with the regular class instance variables.
Would you like to share the reason(s) to use the member variables?
I am having the same problem and in my case I am usign the hasmaps to get the values from database, do some processing on it and then pass it on.
I am using a single method. And within this single method, my instance variable does not maintain values for individual client.
Any help to resolve above problem is appreciated.
Thanks and Regards,