users@glassfish.java.net

Re: Cannot lookup an EJB in JNDI from a servlet.

From: <glassfish_at_javadesktop.org>
Date: Tue, 22 May 2007 10:12:39 PDT

Why do you need JNDI lookup to get EJB?
Just use injection.

Example how I use it:

import com.medenterprise.app.patientservice.session.PatientService;

public class PatientServiceController {
        private @EJB PatientService patientService;

-------------------------------------------------------
PatientService.java :

@Local
public interface PatientService {
------------------------------------------------------

If you still need JNDI lookup I will show again example from my code:

package com.medenterprise.app.patientservice.session;

import javax.ejb.Stateless;
import com.medenterprise.app.patientservice.persistence.Patient;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.persistence.EntityManager;

@Stateless
public class TestServiceBean implements TestServiceLocal {
  @Resource SessionContext context;
  
  public TestServiceBean() {
  }
  
  public Patient findPatient(String databaseName, int id) {
    EntityManager em = EntityManager.class.cast(context.lookup(databaseName));
    return em.find(Patient.class, id);
  }
}

Using SessionContext class is better than use InitialContext class.
By the way with InitialContext you have to use prefix "java:comp/env"
Maybe that the reason why your code doesn't work.
I didn't use much JNDI so I could be wrong somewhere :)

Good luck,
Vladimir
[Message sent by forum member 'vladperl' (vladperl)]

http://forums.java.net/jive/thread.jspa?messageID=218377