Use the ejb-jar.xml to specify what is in your ENC and jboss.xml to map it to the JNDI name.
For example, to have access to the default datasource, java:/DefaultDS, you would use the following:
<ejb-jar ...> <enterprise-beans> <session> <ejb-name>MySessionBean</ejb-name> <local>my.local.MySessionLocal</local> <ejb-class>my.bean.MySessionBean</ejb-class> <resource-ref> <res-ref-name>jdbc/MyDataSource</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </session> </enterprise-beans> </ejb-jar>
<jboss ...> <enterprise-beans> <session> <ejb-name>MySessionBean</ejb-name> <resource-ref> <res-ref-name>jdbc/MyDataSource</res-ref-name> <resource-name>DefaultDS</resource-name> </resource-ref> </session> </enterprise-beans> </jboss>
and perform the lookup within MySessionBean as:
DataSource ds = (DataSource)new InitialContext().lookup("java:comp/env/jdbc/MyDataSource");
This is certainly how I'd do it in 2.1, but is it not simpler in 3?
What is the benefit to putting the references into the local ENC anyway - all the EJB 3 examples always seem to use the global look up.
the ejb3 way is to use
@Resource(mappedName="DefaultDS") private DataSource ds;
Indeed, but is the ref then within the local ENC?
And what if u want to use the ref in a pojo - doesnt Jboss 4.2.0 not allow you to use the resource ref in anything other than an EJB?
the mappedName refers to java:/DefaultDS, which is not in the bean's ENC.
nothing is put in the ENC automatically, if you MUST have it in the ENC then you will need to add it with deployment descriptors or with @EJB at the class level.
Thanks for your replies thus far.
What I've never understood is the benefit to having it in the local ENC? Why is this generally done?
by using the ENC you're not coupling you code to the app. server (there are no JBoss specific JNDI names). you will have to include an app. server specific dep. desc. (jboss.xml), but once you're code is compiled you're good to go as far as portability. if you wanted to change to Glassfish, for example, you would just replace the jboss.xml with an equivalent Glassfish dep. desc..
I'm still not satisifed I know why there is a global directory and a local/ENC, and why you should use the latter?
global JNDI names are app. server specific--the default global name for a bean deployed to Glassfish will not be the same as if it were deployed to JBoss.
if you don't care about hard-coding JBoss specific names into your classes, then there's no need to worry about it.