Stateless Session Beans vs. Singleton Session Beans

23,204

Solution 1

I would go for Stateless - the server can generate many instances of the bean and process incoming requests in parallel.

Singleton sounds like a potential bottleneck - the default @Lock value is @Lock(WRITE) but may be changed to @Lock(READ) for the bean or individual methods.

Solution 2

according to the ejb 3.1 spec, page 110, chapter 4.8.5 "Singleton Concurrency":

It is legal to store Java EE objects that do not support concurrent access (e.g. Entity Managers, Stateful Session Bean references) within Singleton bean instance state. However, it is the responsibility of the Bean Developer to ensure such objects are not accessed by more than one thread at a time.

and furthermore, according to the hibernate entitymanager documentation

An EntityManager is an inexpensive, non-threadsafe object that should be used once, for a single business process, a single unit of work, and then discarded.

For me, this means, that you should never inject an EntityManager into a singleton EJB. I would use a singleton EJB as a replacement for a stateless EJB only if EVERYTHING I need to implement in this class supports concurrency without the need to do additional locking / synchronization. As you or other programmers might lose this issue sooner or later from your focus, I personally prefer to not use singleton EJBs except for startup-related issues or features that can be implemented as self-contained units - independently of other beans. In that sense, it doesn't seem to be advisable to inject for example Stateless EJBs into Singletons. Doing so raises the question about the point in time, when the container actually performs the injection of the SLSB into the Singleton? According to the EJB 3.1 Spec, chapter 4.8, the dependency injection gets done before the singleton bean instance can be accessed by clients. So the singleton would obviously stick to the same instance of the SLSB, which seems to become a singleton implicitly, but there doesn't seem to be any guarantee for that. At least I couldn't find anything in the specs, so the behavior might be unpredictable or in the best case container-specific, which is not what most people will want.

Thus, I would only inject Singletons into Singletons or Singletons into SLSBs but not vice versa. For the case of an injection of a Singleton into a Singleton, the Spec offers you the opportunity to define the dependencies between the singletons so that the container can initialize them in the correct order (see the ejb 3.1 spec, chapter 4.8.1 concerning the @DependsOn annotation).

Solution 3

@Stateless will allow you to have multiple copies ready for processing within a JVM (as much as memory and pool size allows) where-as @Singleton there's only one copy in a JVM, even if the single one can support multiple concurrent threads running against it.

In terms of performance @Singleton would be better, provided that the resources it uses allow long running access. However, in a distributed environment sometimes bad things occur, e.g. database or network links may fail.

With a @Stateless bean, the access is more short lived. In addition, should there be a failure it will just respawn and try to establish a new connection to the resource. If something happens like that on a singleton, then it's up the the singleton to handle it without requiring an application restart because the @PostConstruct is only called once per JVM.

I would prefer a bit of fault tolerance vs performance for most situations especially on systems I have no control over.

Solution 4

I think Singleton in concurrency usage will not perform worse than SLSB Pool, it might be even better. The only problem is if you want to share something between threads, you need lock it, and that could be a big problem of performance. So in that case, a SLSB Pool perform much better, because it's not 100% singleton, there are more instances, one got locked, the other one comes up. Anyway if the lock is on some resource sharing by all SLSBs, the pool won't help neither.

In short, I think singleton is better than SLSB Pool, you should use it if you can. It's also the default scope for Spring Beans.

I'm not a JavaEE expert, that's just my feeling, please correct me if I'm wrong.

Share:
23,204
deamon
Author by

deamon

Updated on June 16, 2020

Comments

  • deamon
    deamon about 4 years

    The Java EE 6 Tutorial says:

    To improve performance, you might choose a stateless session bean if it has any of these traits:

    • The bean’s state has no data for a specific client.
    • 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.
    • The bean implements a web service.

    Singleton session beans are appropriate in the following circumstances:

    • State needs to be shared across the application.
    • A single enterprise bean needs to be accessed by multiple threads concurrently.
    • The application needs an enterprise bean to perform tasks upon application startup and shutdown.
    • The bean implements a web service.

    But what to use if:

    • no state has to be shared across the application
    • a single enterprise bean could be accessed by multiple threads concurrently
    • no tasks on startup or shotdown need to be performed

    Say for example I have a login service with the following interface:

    public interface LoginService {
      boolean authenticate(String user, String password);
    }
    

    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)?

    Addition: 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?

  • Admin
    Admin over 9 years
    Could you explain, why you consider singleton like a bottleneck if we can use @Lock(LockType.READ) for all class? For example @Lock(LockType.READ)public class MySingleton {..}.
  • DavidS
    DavidS about 8 years
    I wonder if this is actually a drawback to Stateless, mjn. With Stateless, do we need a pool size of N to serve N concurrent requests? Presumably a Singleton with a @Lock(READ) can serve N concurrent requests without adjusting pool size.