0 Replies Latest reply: Apr 9, 2012 9:33 AM by Mohamed Mostafa RSS

Context Lookup error

Mohamed Mostafa Newbie

i will explain my project breifly

 

I need to create a webservice which calls an EJB that execute a query on a sybase database

this database is a datasource on glassfish server ( i am using Netbeans 7.1.1 )

 

i have created Stateless session EJB which i needs it to connect to the database using JNDI

 

here is a part of the class

 

@Stateless
public class NewSessionBean implements NewSessionBeanLocal {
    @Resource(name = "ejb")
    private DataSource ejb;

    @Override
    public String execute(String query) {
        String dbName = "java:comp/env/jdbc/ejb";
        try {
            Context loContext = (Context) new InitialContext().lookup("java:comp/env");
            ejb = (DataSource)loContext.lookup("jdbc/ejb");
            Connection loConnection = ejb.getConnection();

 

and the glassfish.xml found in the configuration files of the EJB project is

 

<glassfish-ejb-jar>
  <enterprise-beans>
    <ejb>
      <ejb-name>NewSessionBean</ejb-name>
      <resource-ref>
        <res-ref-name>ejb</res-ref-name>
        <jndi-name>jdbc/ejb</jndi-name>
      </resource-ref>
    </ejb>
  </enterprise-beans>
</glassfish-ejb-jar>

 

when i run the code it always gives an error at that line

 

Context loContext = (Context) new InitialContext().lookup("java:comp/env");

 

i have tried many lookups like

 

Context loContext = (Context) new InitialContext().lookup("java:comp/env/jdbc/ejb");

 

HINT: my JNDI name i want to use is: jdbc/ejb

 

always giving me that error

 

run:

Apr 9, 2012 3:26:35 PM bean.NewSessionBean execute

SEVERE: null

javax.naming.NamingException: Lookup failed for 'java:comp/env' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]

    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)

    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)

    at javax.naming.InitialContext.lookup(InitialContext.java:392)

    at bean.NewSessionBean.execute(NewSessionBean.java:35)

    at bean.Main.main(Main.java:14

)

Caused by: javax.naming.NamingException: Invocation exception: Got null ComponentInvocation

    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.getComponentId(GlassfishNamingManagerImpl.java:873)

    at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:742)

    at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:177)

    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)

Please anyone help me if anything still unclear i will provide you with all you need i need this solution urgently.