Question

With a web service defined like @Stateless

 import javax.ejb.Stateless;
 import javax.jws.WebService;

 @Stateless
 @WebService(serviceName = "TestService")
 public class TestService {
     int counter = 0;
     public int getCounter() {
         return counter++;
     }
 }

Why 'counter' is increased with each request and does not return always 0?

Was it helpful?

Solution

Because with @Stateless you're telling the container that you are not holding any state, but you do hold state.

With @Stateless the container only creates one instance of the bean, because there's no need to create more.

You might want to read a bit more about JEE and what the annotations mean: http://theopentutorials.com/tutorials/java-ee/ejb3/session-beans/slsb/introduction-11/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top