4

I know there's a lot of debate on using stateful vs stateless EJBs in web applications.

The shopping cart is the most common use case: Oracle's Java EE examples use it a lot in the official docs, too.

Here on stackoverflow I found many interesting answers like this The Shopping Cart dilemma in JavaEE which often say something like:

ok... SFSB are good in enterprise, complex scenarios, e.g. if you want to share them with other applications and make them available not only to JSF/web clients

but... if you're just developing your grandpa's e-commerce website, just stick to the HttpSession / SessionScoped cdi-managed bean, and write your business methods in SLSB, as they are more efficient, and so on...

However, because I'm still in a learning and discovery phase, I just want to give SFSB a try, by myself, trying to build a simple shopping cart.

I saw an interesting tutorial suggesting to store a JNDI-retrieved instance of the @Stateful shopping cart ejb interface in the HttpSession, the first time the web client needed it, then use it as usual, during the web session. In my JSF presentation layer, I suppose I would have a @SessionScoped @Named bean (let's call it ShopController), and, in its initialization, store one instance of the stateful ejb in an instance variable.

I wonder if it's possible to directly bind the @Stateful bean to the http session by annotating it with the @SessionScoped CDI annotation.

Will it work as described above? Will CDI create one SFSB for each web session?

Fabio B.
  • 9,138
  • 25
  • 105
  • 177
  • Yes you can but after session times out I get "java.rmi.NoSuchObjectException: Not Found" in TomEE. So it won't recreate the session EJB for the same user. – Panu Haaramo Dec 31 '17 at 16:13

2 Answers2

6

@SessionScoped is for @Named beans and @Stateful is for @EJB beans. If I'm not wrong, you cannot annotate 1 bean with both. If you want to use @Stateful, just annotate your ShoppingCart bean with @EJB and @Local and then reference it in your ShopController. Something like this:

@Named
@SessionScoped
public class ShopController {
    ...
    @EJB
    private ShoppingCart cart;
    ...

    // Getters and Setters
}

@Local
@Stateful
public class ShoppingCart {
    ...
}
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • uhm... I think you didn't understand my question. I'm trying to bind my Stateful bean (I already know how to create and manage it) to my http session, what's the best practice? I mean, does the way you suggest (ShopController session scoped) inject the shoppingcart ejb each time is initialized? – Fabio B. Apr 01 '14 at 14:09
  • In my solution, the both the `ShopController` and `ShoppingCar` beans are only created once, at the beginning of the session. – Mr.J4mes Apr 01 '14 at 14:48
  • 1
    could you please edit with a more complete code example? – Fabio B. Apr 01 '14 at 14:56
0

Don't waste your time learning how to use SFSB for web applications. You will have scalability issues very soon. Why would you learn how to make an application that uses unnecessary server resources?

Even your managed beans shouldn't be SessionScoped. At most create only one very thin SessionScoped MB with small user data to track it and all others should be request, view scoped.

The answer to your question is yes, you can use CDI to bind SessionScoped MB to SFSB EJBs. But this is not a nice architecture for web applications.

Yamada
  • 723
  • 6
  • 23
  • 3
    Could you please enrich your answer and add more details on why I will have scalability issues using sfsb? Then, let's say I believe you. However, my web app is an e-commerce website. In this kind of project I always take advantage of the http session for storing info such as login/user,shopping cart and so on. What do you mean "a thin session scoped MB" ? What do you suggest? Please complete your answer, thank you. – Fabio B. Apr 02 '14 at 06:35
  • It would nice to support such a straight-out answer with references to articles/post/researches. Your point is questionable though. May the problem is not with the SFSB but with the wrong use of it? You may find some thought about it in the following link and this is not the only place. http://www.theserverside.com/tutorial/Which-EJB-to-use-Stateful-stateless-and-singleton-session-beans-compared. – jjd Apr 02 '14 at 09:23
  • The point is very questionable. While there is a relationship between statelessness and scalability, it's far more complex than "If you want to scale, be stateless." Without context "scale" is a practically meaningless word. In some contexts, SFSB can be successfully used with hundreds or thousands of concurrent users. "Web application" does not mean millions of concurrent internet users: in many cases it's hundreds of concurrent enterprise users (e.g. Atlassian JIRA). – DavidS Apr 28 '15 at 21:13