-1

Spring has singleton and prototype. EJB has singleton and stateless.

I've been working on EJB since quite some time and I was of the opinion that singleton in EJB means multiple users cant access the class at the same time. And hence users will be kept waiting until the resource is free. Hence always used stateless.

Coming to Spring there is no equivalent of EJB stateless. Everything is singleton by default or new if used as prototype. So what happens when multiple users are trying to access the same singleton bean concurrently. Are they kept waiting?

Maybe my Java fundamentals are not clear here. What happens when multiple users try to access the same java singleton class or same method of the same java singleton class. How does it work internally? By java singleton here, I mean a class with a private constructor.

Is using Singleton and Stateless in EJB one and the same?

wib
  • 425
  • 3
  • 17
  • A private constructor does not necessarily make a class a singleton, and multiple threads can indeed invoke a singleton at the same time. See also [Double-checked locking: Clever, but broken](http://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html) (from 16 years ago!) – Elliott Frisch May 26 '17 at 00:06

3 Answers3

2

I've been working on EJB since quite some time and I was of the opinion that singleton in EJB means multiple users cant access the class at the same time. And hence users will be kept waiting until the resource is free. Hence always used stateless.

Multiple threads can use same singleton bean at same time. https://docs.oracle.com/cd/E19798-01/821-1841/gipsz/index.html

Coming to Spring there is no equivalent of EJB stateless.

Maybe there's no annotation, but the behavior is the same.

Maybe my Java fundamentals are not clear here. What happens when multiple users try to access the same java singleton class or same method of the same java singleton class. How does it work internally? By java singleton here, I mean a class with a private constructor.

As other users said, a class with a private constructor isn't necessarily a singleton. My preferred singleton is:

    private static final UserType INSTANCE = new UserType();

    private UserType(){
    }

    public static UserType get(){
        return INSTANCE;
    }

Multiple threads can use java singleton, there's no synchronization after instantiation.

Is using Singleton and Stateless in EJB one and the same?

Absolutely not. Stateless beans have a pool of instances. Singleton beans have only one, as java singleton objects. But about concurrency, yes, both will not block threads.

Gilliard Macedo
  • 386
  • 1
  • 6
  • so whats the advantage of having stateless in EJB? If it had any advantages then there would have been high chance of Spring having it as well – wib May 27 '17 at 06:05
  • "stateless" is a concept about have no state, i.e., no instance data, that Prototype Spring Beans also have. If you put instance data in a Stateless EJB or Prototype Spring Bean, you can't acess the data after the end of request. The diference is prototype Spring Beans are created every request, while Stateless EJBs maintain a pool. – Gilliard Macedo May 29 '17 at 18:40
1

I assume you are talking about a Web application. When a web application running in a servlet container, each user request creates a separate thread with its own Stack. Therefore, methods of a singleton class can be executed by multiple concurrent threads. This is the beauty of Java Virtual machine. This allows concurrent users to access same code without blocking each other.

SAP
  • 468
  • 2
  • 14
1

So what happens when multiple users are trying to access the same singleton bean concurrently. Are they kept waiting?

Don't mix up synchronized with Singletons. Of course, multiple threads can access a Spring Bean the same time.

What happens when multiple users try to access the same java singleton class or same method of the same java singleton class.

Nothing, but if your Singleton class has instance or class variables, the threads will access them concurrently.

Jan B.
  • 6,030
  • 5
  • 32
  • 53
  • so using Singleton and Stateless in EJB is one and the same? – wib May 26 '17 at 01:47
  • 1
    No, there are a lot of differences. A Singleton is just a design pattern ensuring a single instance per classloader. Stateless EJBs can be created multiple times (see EJB pooling). Furthermore, EJB do a lot of work for you like dependency injection etc. When you go for EJB or Spring, you probably shouldn't use Singletons anymore. – Jan B. May 26 '17 at 10:28
  • What is the advantage of Statelest EJB vs Singleton EJB then? – wib May 29 '17 at 04:23
  • Singleton Session Beans maintain a state across the whole application, Stateless Session Beans don't. For instance, beans that perform actions on application startup or shutdown are Singleton Session Beans, they should exist only once. Or if you do expensive things like accessing remote services you _could_ use a Singleton that returns cached versions of data (cache=state) for concurrent requests. For other, typically more generic things, like serving client requests, you'd pick Stateless Session Beans. It always depends on the particular use case, though. – Jan B. May 29 '17 at 10:07