JNDI lookup not working with EJB 3.x

11,463

Solution 1

The problem was I was using a Remote Interface. Using simply the @stateless annotation without mapped name The following code worked:

new InitialContext().lookup("java:global/ProjectName/ModuleName/BeanName!FullyQualif‌​iedNameOfRemoteInterface"); 

Thanks @Andre!

Solution 2

It might be a better idea to make use of Portable JNDI Name, i.e. just annotate with @Stateless

https://blogs.oracle.com/kensaks/entry/application_specified_portable_jndi_names

Solution 3

Though this question has been answered two years ago, I would like to add a comment on this. There should have been no problem in using the mappedName attribute. If you were deploying on WebLogic, you have to add #[fully.qualified.interface.name] in your lookup.

e.g. mappedName = "UserBean", the EJB is implementing an interface named MyInterface in the package com.acme.user, then the lookup would be like

... = new InitialContext().lookup("UserBean#com.acme.user.MyInterface");
Share:
11,463
Aditya
Author by

Aditya

Updated on June 04, 2022

Comments

  • Aditya
    Aditya almost 2 years

    I'm using the following Bean class:

    @Stateless(name="UserBean", mappedName="UserBean")
    @LocalBean
    public class User implements UserRemote {
    
    @PersistenceContext
    private EntityManager em;
    
    public User() {
    
    }
    
    public String login(String username, String password) {
    
        Query query = em.createQuery("...");
        return "xyz";
    }
    
    }
    

    And my method is

    public String myMethod()    {
    
        try {
            User user = (User) new InitialContext().lookup("UserBean");
            return "xyz";
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return null;        
    }
    

    Here I'm getting a

    javax.naming.NameNotFoundException: Unable to resolve 'UserBean'. Resolved ''; remaining name 'UserBean'
    

    The JNDI lookup name 'UserBean' seems to be correct. No idea what the problem is. Can anyone please help? I've deployed my application on weblogic 12c, using JPA 2.0 and EJB 3.x

    Thanks in advance.

  • Aditya
    Aditya about 11 years
    I tried with the annotation as just '@Stateless'. My EAR name is Aahar_v.0.1 and my EJB Module name is Aahar_EJB_v.0.1, so I tried the JNDI name as 'java:Aahar_v.0.1/Aahar_EJB_v.0.1/User' and I'm still getting 'javax.naming.NameNotFoundException: Unable to resolve 'java:global.Aahar_v.0.1.Aahar_EJB_v.0.1.User'. Resolved 'java:global.Aahar_v.0.1.Aahar_EJB_v.0.1'; remaining name 'User'' Am I doing something wrong?
  • Andre
    Andre about 11 years
    Do you need a remote interface, i.e. do you need to lookup outside of application server? If not, drop the interface
  • Aditya
    Aditya about 11 years
    The problem was I was using a Remote Interface. Using simply the @stateless annotation without mapped name The following code worked: new InitialContext().lookup("java:global/ProjectName/ModuleName/‌​BeanName!FullyQualif‌​iedNameOfRemoteInter‌​face"); Thanks @Andre!
  • zenbeni
    zenbeni about 2 years
    link is dead, you should have provided a summary of what is important there