1

I was reading a book regarding EJB 3.0 and it says that stateful session bean should be looked up using JNDI.

I have an stateful session bean as follows:

@Local
@Stateful
public class JpaDao  {

    @PersistenceContext(unitName="EmployeeService")
    EntityManager em;

    public EntityManager getEntityManager() {
        return em;
    }       

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void addEmployee(String name, String lastName) {

        Customer cust = new Customer();
.....................
....

In a JSF managed bean I did as follows:

Context ctx = new InitialContext();
JpaDao jpa = (JpaDao)ctx.lookup("java:comp/env/JpaDao");

But It didnt find anything. Why so ?

I opened wildfly cli client and saw jndi tree: subsystem=naming:jndi-view()

"applications" => {
    "JavaServerFaces-1.0.war" => {
        "java:app" => {
            "AppName" => {
                "class-name" => "java.lang.String",
                "value" => "JavaServerFaces-1.0"
            },
            "env" => {
                "class-name" => "org.jboss.as.naming.NamingContext",
                "value" => "env"
            },
            "JavaServerFaces-1.0" => {
                "class-name" => "javax.naming.Context",
                "children" => {
                    "JpaDao" => {
                        "class-name" => "com.deluxe.common.dao.JpaDao",
                        "value" => "?"
                    },
                    "JpaDao!com.deluxe.common.dao.JpaDao" => {
                        "class-name" => "com.deluxe.common.dao.JpaDao",
                        "value" => "?"
                    }
                }
            }
        },
        "modules" => undefined
    },

It does show an entry for JpaDao, then why JNDI lookup doesn't return anything.

António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
user204069
  • 1,215
  • 3
  • 19
  • 25

1 Answers1

0

I think you should look it up using java:global/.../JpaDAO But I think it is better to use CDI injection to get the bean instead of an explicit lookup. It will also deliver you the bean instance and you won't have to touch the places where you query the bean if anything changes.

The question is why are you using a stateful session bean for your DAO? DAOs are typically stateless since they only persist, ... entities without having any internal state.

shillner
  • 1,806
  • 15
  • 24