0

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?

maksim
  • 458
  • 2
  • 6
  • 16

1 Answers1

1

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/

Augusto
  • 28,839
  • 5
  • 58
  • 88
  • The given link helped to understand it better. Thank you. Especially than i modified `getCounter` method with `Thread.sleep` for easy way to get concurrent access. About "With `@Stateless` the container only creates one instance of the bean, because there's no need to create more" - different instances created in case of such concurrent access. – maksim Feb 13 '14 at 13:37