Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @Stateless and @Singleton

Tags:

ejb

ejb-3.1

I'm following this tutorial which also uses an EJB:

package exercise1;  import java.util.Random; import javax.ejb.Stateless; import javax.inject.Named;  @Stateless public class MessageServerBean {     private int counter = 0;      public String getMessage(){         Random random = new Random();         random.nextInt(9999999);         int myRandomNumber = random.nextInt();         return "" + myRandomNumber;     }      public int getCounter(){         return counter++;     }     } 

Here is an output example:


Hello from Facelets
Message is: 84804258
Counter is: 26
Message Server Bean is: exercise1.MessageServerBean@757b6193


Here's my observation:

  • When I set the bean as @Stateless I always get the same object ID and counter always increments.
  • When I set the bean as @Stateful I get a new instance every time I refresh the page.
  • When I set it to @Singleton I get the same results as when I set it to @Stateless: same object ID, counter incrementing.

So, what I actually would like to understand is: what's the difference between @Stateless and @Singleton EJBs in this very case?

like image 381
godzillante Avatar asked Jan 22 '13 17:01

godzillante


People also ask

Is singleton stateless or stateful?

A stateless singleton is pretty much a collection of static methods; it's no different from a static util class, and it doesn't really matter how many instances there are: 0, 1, 2 or infinity. Therefore singletons are usually stateful.

Is Ejb a singleton?

The javax. ejb. Singleton annotation is used to specify that the enterprise bean implementation class is a singleton session bean: @Singleton public class SingletonBean { ... }

What is the difference between stateful session beans and stateless session beans Mcq?

The main difference between Stateless and Stateful Session Bean is that Stateless Session Bean is a business object without state (data) that describes the business logic while Stateful Session Bean is a business object with a state (data) that describes the business logic.

What is a stateful bean?

What is a Stateful Session Bean? A stateful session bean is a session bean that maintains conversational state. Stateful session beans are useful for conversational sessions, in which it is necessary to maintain state, such as instance variable values or transactional state, between method invocations.


1 Answers

You're seeing the same output because there is only one client accessing the EJB at a time. The application server is able to recycle the same stateless EJB object for each call. If you try a concurrent access – multiple clients at the same time - you'll see new stateless instances appearing.

Note that, depending on the server load, even two consecutive method invocations made by the same client may end up in different stateless EJB objects!

For a singleton EJB, there will no difference – there is always only one instance per application, no matter how many clients try to access it.

like image 76
gcvt Avatar answered Sep 22 '22 15:09

gcvt