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 6473721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:30:01+00:00 2026-05-25T06:30:01+00:00

I never used the @Singleton new feature of JavaEE 6 and i want to

  • 0

I never used the @Singleton new feature of JavaEE 6 and i want to give it a try.

I was thinking in creating s Singleton to just hold a password that will allow the app adiministrator(The person that knows the password),to access some content of the app.

I tried to implement it following this tutorial, but it does not work.
This is what i did:

I created the singleton bean:

@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class AdminAcountEJB implements IAdminAcountEJB {

    private String password;

    @PostConstruct
    public void init() {
        password = "password";
    }


    @Lock(LockType.READ)
    public String getPassword() {
        return password;
    }
}

I extracted an interface

public interface IAdminAcountEJB {

    public abstract String getPassword();
}

Then i try to inject the singleton in a managed bean using @EJB

@Named("managementBB")
@SessionScoped
public class ManagementBB implements Serializable{

    @EJB
    private IAdminAcountEJB managementEJB;    
    private String input;       
    private boolean authorized;

    public String seeWhatsUp() {
        if(input.equals(managementEJB.getPassword())) {
            authorized = true;
            return "manage?faces-redirect=true;";
        }
        return "index?faces-redirect=true;";
    }

  //Get set methods...

}

The last thing i do is create some markup that is displayed in case the correct password is entered:

<h:form rendered="#{managementBB.authorized == false}">
            <h:inputSecret value="#{managementEJB.input}"/>
            <h:commandButton value="..." action="#{managementEJB.seeWhatsUp}"/>
        </h:form>

<h:form rendered="#{managementBB.authorized}">  
             CORRECT PASSWORD!!
</h:form>

It all seems ok to me, but when i access the page, the console says:

javax.naming.NameNotFoundException:
ejbinterfaces.IAdminAcountEJB#ejbinterfaces.IAdminAcountEJB not found

I don’t understand why it don’t work, this is how i inject other EJB’s that are not Singletones but with @Singleton it does not work.

-How can i fix it?

-I am also interested in knowing what do you think about using a singletone for this purpose, you think is a good and safe idea?

  • 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-25T06:30:02+00:00Added an answer on May 25, 2026 at 6:30 am

    I guess the problem is, that since you refer to your singleton within an EL expression in the view, it has to be annotated with @Named. If you use your beans only within others, this is not necessary.

    Concerning your design, my 2 pennies are these:

    • Since you are using Java EE 6, you won’t need to specify an interface for it. If you want/need it nevertheless, don’t call it ISomething (except you work for Apple 😉 but give it a domain related name.
    • Using a singleton which allows concurrent read access is ok for core data. Only, I wouldn’t put the password within the code, but into a database table, preferrably hashed and use the singleton as a provider for that which reads the table at startup.
    • Singletons in general may always introduce a bottleneck into the application because by definition they don’t scale. So for your use case it’s ok, since we can assume the access rate is very low. The other problem which might be introduced are race conditions (also not in your case) if we have data that changes, since we only have one instance being called in parallel.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Never used a cache like this before. The problem is that I want to
I've never used SOAP before and I'm sort of new to Python. I'm doing
I never used ruby before, I just wanted to play around with HAML and
I have never used enums before i have a problem while creating enums i
I've never used a MySQL API before. I read about the oursql API that
Never used fwrite(), so I'm not sure exactly what I'm doing wrong. I'm just
Disclaimer: Never used php in my life. New to Unix and Jenkins I have
Background/Question: I'm fairly new to the singleton design pattern. I've used it once in
never used log4net before, but I may end up kicking myself for that... if
I have never used SN or Git but I really want to start. I

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.