user714154
user714154

Reputation: 21

java:comp/env is not found when using openejb in embedded mode

as openejb documentation decalres

The unbreakable rules. Read these over and over again when things don't work.

  1. java:comp/env is the spec defined namespace for lookup of any Container-Managed Resource
  2. java:comp/env is empty by default
  3. java:comp/env is read-only at runtime java:comp/env is populated by Declaring References to Container-Managed Resource via xml or annotation

i'm using openejb in embedded mode, DI is working fine, but i want to make lookup in pojo to get a reference to my Data Source using the standard jndi lookup as ctx.lookup("java:comp/env/DS") i tried to declare resources via xml and @Resource in a stateless ejb just to test if the env subcontext is populated but i don't know why the env subcontext is never created ... plz help

Upvotes: 2

Views: 2416

Answers (1)

Jakub Marchwicki
Jakub Marchwicki

Reputation: 868

I had similar issues when I was using OpenEJB in tests. In case of embedded tests with OpenEJB you should rather look for java:openejb/ not java:comp/env

With this little snippet you can list what's registered by OpenEJB

NamingEnumeration<Binding> list = initialContext.listBindings("java:openejb/");
while (list.hasMore()) {
    Binding item = list.next();
    System.out.println(item.getClassName() +" :: " + "java:openejb/" + item.getName());
}

if you want to get to your datasource list everything registered under "java:openejb/PersistenceUnit/". most probably you will find "java:openejb/PersistenceUnit/[name-of-persistence-unit] [hashcode]" - whicho you can use later on - in a test.

hope that helps --Jakub

Upvotes: 5

Related Questions