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

  • Home
  • SEARCH
  • 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 7569009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:04:15+00:00 2026-05-30T15:04:15+00:00

I have an EJB that needs to be a singleton and stateful since it’s

  • 0

I have an EJB that needs to be a singleton and stateful since it’s going to be a sort of connection pool. My questions are:

  1. If I define an EJB with @Singleton annotation, will it then be stateful by default or do I have to define it with @Stateful also?
  2. Can it be annotated with @Stateless?

Tried to find some documentation about this but no luck so far. So anyone with knowledge, please share your wisdom and perhaps a link or two.

  • 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-05-30T15:04:16+00:00Added an answer on May 30, 2026 at 3:04 pm

    The EJB tutorials show that an EJB can be either Singleton or Stateful or Stateless. I have never tried to use more than one of these annotations, but I am fairly convinced that the right thing to do is to use only one of them.

    From that link:

    Singleton session beans maintain their state between client
    invocations

    So, to your question:

    if I define a EJB with @Singleton annotation will it then be stateful by default or do I have to define it with @Stateful also?

    If for Stateful you mean the ability to maintain its state, the answer is: yes, a Singleton will be Stateful by default.

    Keep in mind that there are some particular situations in which a Singleton doesn’t behave like a Singleton, read this article about this. Generally, you don’t run this kind of risk if you are outside of a cluster and avoid using the default constructor: you need to always use references of an EJB by injecting it in another EJB or a web client using:

    @EJB MyEJB myEJB;
    

    Finally, have a look at this part of the Java EE 6 tutorial, about EJBs lifecycle, explaining that the main difference between Stateful and other EJBs is the ability to being passivated by the container during its life. This difference is the main reason why the statement "a Singleton is Stateful by default" is not correct strictly speaking, but is correct in the context of your question.

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

Sidebar

Related Questions

We have some JavaEE5 stateless EJB bean that passes the injected EntityManager to its
I have a large application that uses EJB 2.x entity beans (BMP). This is
I have a remote EJB with a method that validates an object (returning true
I have an old EJB (2.1) project that uses xdoclet (1.2.3) to generate the
We currently have a Stateful bean that is injected into a Servlet. The problem
I have a stateless EJB that acceses my database. I need this bean in
I have a J2EE application that needs to be distributed to customers, who will
Using jboss-esb 5.1.0.GA I have a web service that an EJB that I have
We have a Java EE application running on Glassfish 3.1 that needs to accept
We have an application that needs to access a database that is owned by

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.