1

I have a stateless EJB:

@Stateless
public class DefaultStatelessService implements StatelessService {

    private Object o;

}

where I use provate field o in few private methods. The question is: after the stateless will finish it's job will its state be cleared?

For example:

  1. Stateless instance has been taken from pool.
  2. State of private field o has been changed during stateless job.
  3. Stateless instance has been returned to pool.
  4. Some other operation taken the same stateless instance and want to use it - is object o in the same state as before or it has been cleared?
pepuch
  • 6,346
  • 7
  • 51
  • 84
  • Why does your stateless service have a state? – isnot2bad Aug 12 '14 at 08:01
  • I did it in this way because few private methods use this object so I though it will be easier to use it as private field than each method parameter. – pepuch Aug 12 '14 at 08:04
  • I don't think the field will be cleared, however you musn't store a data related to a specific user in a stateless EJB. This other question might give you some information http://stackoverflow.com/questions/2351220/stateless-and-stateful-enterprise-java-beans. – Dici Aug 12 '14 at 08:10

1 Answers1

1

You cannot rely upon any state stored in a stateless session bean between method invocations.

Some container implementations may not even bother to use a pool - it will just give you a new instance each time.

In your example there is nothing that will explicitly clear "o", but it's quite possible the entire bean instance will be discarded.

Steve C
  • 18,876
  • 5
  • 34
  • 37