Java – stateless session bean with instance variables
I have a stateless session bean that contains a public method, several private methods, and some instance level variables The following is a pseudo - code example
private int instanceLevelVar public void methodA(int x) { this.instanceLevelVar = x; methodB(); } private void methodB() { System.out.println(instanceLevelVar); }
What I see is that methodb prints values that are not passed to methoda Preferably, I can tell it to print values from other instances of the same bean What will happen?
I should point out that the code works 99.9% of the time However,. 01% caused some serious problems / doubts to me
I understand that if I have different public methods, I may not return the same bean between calls, which will lead to this behavior However, in this case, the only call is a single public method Does the container (GlassFish in this case) still exchange beans between private method calls?
(Edit) I renamed "class level" to "instance level" because it caused some confusion
Solution
I don't use instance variables in stateless session beans at all Whatever the reason for your problem, it may not be what you want to do Just try to use local variables or define instance variables in the helper class you call from the stateless session bean business method