title: Stateless Session Beans vs. Singleton Session Beans
body: <p>The <a href="http://java.sun.com/javaee/6/docs/tutorial/doc/gipjg.html#gipmt" rel="noreferrer">Java EE 6 Tutorial</a> says:</p>
  
  <blockquote>
    <p>To improve performance, you might choose a stateless session bean if it has any of these traits:</p>
    
    <ul>
    <li>The bean’s state has no data for a specific client.</li>
    <li>In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an email that confirms an online order.</li>
    <li>The bean implements a web service.</li>
    </ul>
    
    <p>Singleton session beans are appropriate in the following circumstances:</p>
    
    <ul>
    <li>State needs to be shared across the application.</li>
    <li>A single enterprise bean needs to be accessed by multiple threads concurrently.</li>
    <li>The application needs an enterprise bean to perform tasks upon application startup and shutdown.</li>
    <li>The bean implements a web service.</li>
    </ul>
  </blockquote>
  
  <p>But what to use if:</p>
  
  <ul>
  <li>no state has to be shared across the application</li>
  <li>a single enterprise bean could be accessed by multiple threads concurrently</li>
  <li>no tasks on startup or shotdown need to be performed</li>
  </ul>
  
  <p>Say for example I have a login service with the following interface:</p>
  
  <pre><code>public interface LoginService {
    boolean authenticate(String user, String password);
  }
  </code></pre>
  
  <p>Should it be annotated with @Singleton or @Stateless? What are the benefits of the one and the other? What if LoginService needs to get injected an EntityManager (which would be used concurrently)?</p>
  
  <p><strong>Addition:</strong> I'm thinking about the Java EE counterpart of Spring service beans, which are stateless singletons. If I understand that correctly the Java EE counterpart are @Stateless session beans and @Singleton Beans are used to configure the application at startup or cleanup at shutdown or to hold application wide objects. Is this correct?</p>