Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NameAlreadyBound exception when using InitialContext lookup

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used Frits' example on p.66.
I have :




The ejb-jar.xml in echoejb.jar


InitialContext cannot lookup the environment entry at java:global/echoear/echoejb/TheFirstBean/com.ivan.scbcd6.FirstBean/i

Within Second Bean, injected i to the First Bean 300
Info: Within Second Bean, get i in First Bean by look up 300
Info: Within OtherServlet, look up the first bean's i is : 300

Severe: javax.naming.NamingException: Lookup failed for 'java:global/echoear/echoejb/TheFirstBean/com.ivan.scbcd6.FirstBean/i' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameAlreadyBoundException: TheFirstBean]
....
Caused by: javax.naming.NameAlreadyBoundException: TheFirstBean
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2534
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The integer i is only known in the context of TheFirstBean. A lookup from outside the EJB is not possible.
 
Ivan Pyshko
Greenhorn
Posts: 10
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With all the respect , possibly, this is not correct or container dependent, I dunno.
The <env-entry-name> element is relative to the "java:comp/env" JNDI tree node ,
this is why the Integer i is not only known in the bean contexts but outside bean as well.

The code below show all env variable lookup names


In servlet instead of using
(Integer)context.lookup("java:global/echoear/echoejb/TheFirstBean/com.ivan.scbcd6.FirstBean/i");
has to be something like JNDI tree "java:comp/env"
Integer passphrase = (Integer) jndiContext.lookup("java:comp/env/com.ivan.scbcd6.FirstBean/i");

you could even do in servlet something like this.

 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ivan Ka. Thanks for your suggestion.
I think your solution work only when the servlet is in the same .jar file as the beans.

@Resource(lookup= "java:comp/env/com.ivan.scbcd6.FirstBean/i"")
private Integer integer;


I remember the fact that all components in the same .jar share the same JNDI naming environment java:comp/env .

I will try it out later.
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Ivan Ka,
I tried this:
1. In echoejb.jar file, there is a FirstBean, FirstBI ..... And there is a otherweb.war to access the bean in echoejb.
See the attached file structure.

2. Deployment descriptor for FirstBean to inject the integer i:

<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<display-name>echoejb </display-name>
<enterprise-beans>
<session>
<ejb-name>TheFirstBean</ejb-name>
<ejb-class>com.ivan.scbcd6.FirstBean</ejb-class>
<session-type>Stateless</session-type>
<env-entry>
<env-entry-name>com.ivan.scbcd6.FirstBean/i</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>1300</env-entry-value>
<injection-target>
<injection-target-class>com.ivan.scbcd6.FirstBean</injection-target-class>
<injection-target-name>i</injection-target-name>
</injection-target>
</env-entry>
</session>
.....


3. FirstBean.java:


4. SecondBean.java


5. I got this when I access the servlet:
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/com.ivan.scbcd6.SecondBean/secondMyInt'
....
Caused by: javax.naming.NamingException: Lookup failed for 'java:module/TheFirstBean!com.ivan.scbcd6.FirstBI/i'
...
Caused by: javax.naming.NameNotFoundException: No object bound to name java:module/TheFirstBean!com.ivan.scbcd6.FirstBI/i
....

It won't work if I try the lookup="java:comp/env/....

Like Frit said, the integer i in FirstBean is only accessible within the FirstBean, not outside it.



WebApp.png
[Thumbnail for WebApp.png]
the structure of the otherear
fileStructure.png
[Thumbnail for fileStructure.png]
The structure of the echoear.
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried Ivan Ka's code to print out the naming enumeration;


For my FirstBean, the naming enumeration is only this :java:comp/env/com.ivan.scbcd6.FirstBean. The i is not in the enumeration even though my deployment descriptor says this :

<session>
<ejb-name>TheFirstBean</ejb-name>
<ejb-class>com.ivan.scbcd6.FirstBean</ejb-class>
<session-type>Stateless</session-type>
<env-entry>
<env-entry-name>com.ivan.scbcd6.FirstBean/i</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>1300</env-entry-value>
<injection-target>
<injection-target-class>com.ivan.scbcd6.FirstBean</injection-target-class>
<injection-target-name>i</injection-target-name>
</injection-target>
</env-entry>

</session>



My SecondBean has this enumerations:

java:comp/env/com.ivan.scbcd6.SecondBean
java:comp/env/ejb1


My SecondBean has declared an environment entry:


 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to do another experiment. I created a .war file with the FirstBean, SecondBean and the servlet that access the beans.
(See the attached file structure.)



<ejb-jar version="3.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<display-name>echoejb</display-name>
<enterprise-beans>
<session>
<ejb-name>TheFirstBean</ejb-name>
<ejb-class>com.ivan.scbcd6.FirstBean</ejb-class>
<session-type>Stateless</session-type>
<env-entry>
<env-entry-name>com.ivan.scbcd6.FirstBean/i</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>1500</env-entry-value>
<injection-target>
<injection-target-class>com.ivan.scbcd6.FirstBean</injection-target-class>
<injection-target-name>i</injection-target-name>
</injection-target>
</env-entry>

</session>
<session>
<ejb-name>SecondBean</ejb-name>
<ejb-class>com.ivan.scbcd6.SecondBean</ejb-class>
<session-type>Stateless</session-type>
<ejb-ref>
<ejb-ref-name>com.ivan.scbcd6.SecondBean/fbi</ejb-ref-name>
<lookup-name>java:module/TheFirstBean!com.ivan.scbcd6.FirstBI</lookup-name>
</ejb-ref>
<resource-env-ref>
<resource-env-ref-name>java:comp/EJBContext</resource-env-ref-name>
<resource-env-ref-type>javax.ejb.EJBContext</resource-env-ref-type>
<injection-target>
<injection-target-class>com.ivan.scbcd6.SecondBean</injection-target-class>
<injection-target-name>mDDInjectedEjbContext</injection-target-name>
</injection-target>
</resource-env-ref>
</session>
</enterprise-beans>
</ejb-jar>





The servlet can lookup the integer i in FirstBean because in .war file, all the components share the same JNDI environment.
But if the beans are in a .jar file, the servlet in another .war file cannot access the variables in the .jar file.
warFileStructure.png
[Thumbnail for warFileStructure.png]
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2534
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice , well done, and thanks for sharing, have a Cow!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic