Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8189013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:06:18+00:00 2026-06-07T03:06:18+00:00

With EJB (3.x) you have a choice of Session Beans: @Stateful and @Stateless .

  • 0

With EJB (3.x) you have a choice of Session Beans: @Stateful and @Stateless. If I understand the basics behind these two options:

  • @Stateful – Each user/client gets their own and is thus typically not pooled; maintains state across multiple requests
  • @Stateless – Does not maintain state between requests and thus is usually pooled so each new client request gets a fresh bean

My core question here is quite simply, however there are several mini-questions that I have that are tangential to it: how does my POJO design differ between @Stateful beans vs. @Stateless? In other words, if I have a HelloWorld bean that implements the Hello interface, how does that POJO’s design change depending on whether I want it to be stateful or not?

Tangential to this:

  • What different restrictions does the app container (in my case GlassFish) impose upon the EJB depending on whether it is stateful or not?
  • In the case of @Stateful, how does a client-side request from the same user/client get mapped to the correct bean (the one maintaining the client’s state from a previous request)?
  • When do session beans die? I assume its immediately after the request is done for @Stateless, but no clue for @Stateful.

Thanks in advance for any clarity here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:06:19+00:00Added an answer on June 7, 2026 at 3:06 am

    Regarding the core question “how does my POJO design differ between @Stateful beans vs. @Stateless”:

    Your hello world example is an excellent example of a @Stateless session bean: the implementation has no state (no class instance variables). It makes no assumptions about state. If you do need “more information” in a @Stateless bean look it up, (re)compute it, etc. If you break this rule and define instance variables on the bean, you have no assurance that the information will be what you expect it to be on each invocation of the bean: don’t do it.

    Your design of a @Stateful bean would keep internal state (class instance variables) that the container will preserve for the life of the bean; the bean must be Serializable. There are very real run-times costs (memory, and managing the issues around your second tangential question, particularly in a clustered environment) associated with keeping the bean around for the life of the session – so use this only as appropriate (which justifies the existence of @Stateless beans).

    For example, consider a simple sign-up page: enter user name, email address, etc, followed by “are you sure”? A @Stateful bean can preserve this temp information until you’re ready to submit it to a back end.

    Google for examples; here’s one: http://docs.redhat.com/docs/en-US/JBoss_Enterprise_Application_Platform/6/html/Beta_Documentation/Stateful_Session_Bean_Example.html

    HOW the client-side gets mapped to the correct @Stateful bean is truly implementation dependent (not spec’d), however you can expect that it’s managed analogous to how your Servlet Session information is maintained. That said, don’t confuse the Servlet Session with a @Stateful session – they’re not the same thing at all.

    For Java EE 5/6 in your @Stateful bean annotate one method with @Remove, and when your client calls that method it’ll be released back to the ether. Outside of that, as long as you hold a reference to the @Stateful bean (i.e. in your Servlet Session) you can expect that @Stateful bean to be around when you need it.

    See also: http://docs.oracle.com/javaee/5/tutorial/doc/bnbmt.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stateless EJB and two different SessionScoped managed beans. e.g. EJB as
We have some JavaEE5 stateless EJB bean that passes the injected EntityManager to its
I have an application (CC) containing two modules: CC-ejb and CC-war. CC-ejb contains JPA
I have a EJB defined as this: package com.foo; @Stateless (mappedName=HelloWorld) public class HelloWorldBean
I have an EJB that needs to be a singleton and stateful since it's
I'm using IBM RAD 7.5 and WebSphere 7. I have an EJB (@Stateless CustomerService),
I have a EJB bean that exposed to two interface like below: Local interface
I have a large application that uses EJB 2.x entity beans (BMP). This is
I have a two ejb's in an EAR on glassfish, both implementing remote interfaces
I have an EJB which I don't want anyone to be able to instantiate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.