In SCBCD book by Paul Sanghera Page42 (2.7.2 EJB references)
<ejb-ref-type> in <ejb-ref> elements must be session/entity.
My question are 1.why can't we refer MDB here? As we don't have home and local interface for MDB Am I correct?
2.I can use same <ejb-ref> element in web.xml as well as ejb-jar.xml In web.xml (with <ejb-link> to avoid ambiguity and issues due to JNDI name changes In ejb-jar.xml in the declaration on bean if it accesses some other bean e.g. Here EJB1 referes EJB2 so EJB2 is added as <ejb-local-ref> entry.
The ejb-ref tag can be used in both web.xml and ejb-jar.xml with the ejb-link element.
You use it in ejb-jar.xml if you do not know the real JNDI name of the EJB you are referring to. Hence, you cook up a fake JNDI name and annnounce it to the deployer through the ejb-ref tag in the DD so that the deployer can map the fake JNDI name to a real JNDI name during deployment. The ejb-link is optional and will be added by the application assembler if the EJB being referred to is part of the same jar file or the same application (means the EJB referred to is part of the same EAR / JAR file as the EJB referring to it). The ejb-link just helps in quick resolution of the EJB being referred to and the container would not depend on it
You can use the ejb-ref tag in exactly the same way if you are looking up the EJB from some web component (like a Service Locator or even a servlet). After all, some web component has to look up the EJB in a J2EE application, which would typically be a Service Locator. And again you have the same problem, you do not know the real JNDI name of the EJB, so you fake one and announce it in web.xml to the deployer
one additional point: <ejb-ref-name> defined in ejb-jar.xml is unique within that enterprise bean. (i.e Two different enterprise bean declarations can have the same ejb-ref-name without any name conflict).
Whereas the <ejb-ref-name> in web.xml must be unique in the whole web application.
How can 2 EJB's have the same ejb-ref-name in an ejb-jar.xml? A particular ejb-ref-name will be mapped to a particualar EJB's real JNDI name right? That means, an ejb-ref-name in an ejb-jar.xml denotes a single EJB. You probably mean to say that different ejb-jar's in the same application can use the same ejb-ref-name?
<ejb-ref-name> is bound to environment property java:comp/env. And as you know, each EJB has its own private namespace java:comp/env. So you can have duplicate ejb-ref-name in an ejb-jar.xml file. But <ejb-ref-name> has to be unique within the given enterprise bean declaration.
The following example would help: An application has 4 EJBs: EJB1, EJB2, EJB3 and EJB4. Lets say EJB1 references to EJB2 & EJB4. EJB3 references only to EJB4.
Then you can have a deployment descriptor like as follows: //entity description for EJB1 <entity> <ejb-name>EJB1</ejb-name> .... <ejb-ref> <ejb-ref-name>ejb/myEJB1</ejb-ref-name> ..... <env-link>EJB2</env-link> [This is the ejb-name of EJB2] </ejb-ref> <ejb-ref> <ejb-ref-name>ejb/myEJB2</ejb-ref-name> [This name cannot be myEJB1] ..... <env-link>EJB2</env-link> [This is the ejb-name of EJB2] </ejb-ref> ... </entity>
//entity description for EJB3 <entity> <ejb-name>EJB3</ejb-name> .... <ejb-ref> <ejb-ref-name>ejb/myEJB1</ejb-ref-name> ..... <env-link>EJB4</env-link> [This is the ejb-name of EJB4] </ejb-ref> ... </entity> ...
As you can see from the above both EJB1 and EJB3 use the same <ejb-ref-name> "ejb/myEJB1" to refer to different EJBs (EJB2 and EJB4 respectively).
The key points to remember: 1) Each enterprise bean defined in an ejb-jar.xml gets its own private namespace: java:comp/env 2) <ejb-link> is used by application assembler to link the logical name (ejb-ref-name) to the actual referenced ejb.