Part I Development Tasks and Tools
1. Setting Up a Development Environment
Part II Developing Applications and Application Components
6. Using the Java Persistence API
7. Developing Web Applications
8. Using Enterprise JavaBeans Technology
9. Using Container-Managed Persistence
12. Developing Lifecycle Listeners
13. Developing OSGi-enabled Java EE Applications
Part III Using Services and APIs
14. Using the JDBC API for Database Access
15. Using the Transaction Service
16. Using the Java Naming and Directory Interface
Accessing EJB Components Using the CosNaming Naming Context
Accessing EJB Components in a Remote GlassFish Server
Naming Environment for Lifecycle Modules
Built-in Factories for Custom Resources
Disabling GlassFish Server V2 Vendor-Specific JNDI Names
Using Application-Scoped Resources
Using a Custom jndi.properties File
The following XML elements in the GlassFish Server deployment descriptors map resource references in application client, EJB, and web application components to JNDI names configured in the GlassFish Server:
resource-env-ref - Maps the @Resource or @Resources annotation (or the resource-env-ref element in the corresponding Java EE XML file) to the absolute JNDI name configured in the GlassFish Server.
resource-ref - Maps the @Resource or @Resources annotation (or the resource-ref element in the corresponding Java EE XML file) to the absolute JNDI name configured in the GlassFish Server.
ejb-ref - Maps the @EJB annotation (or the ejb-ref element in the corresponding Java EE XML file) to the absolute JNDI name configured in the GlassFish Server.
JNDI names for EJB components must be unique. For example, appending the application name and the module name to the EJB name is one way to guarantee unique names. In this case, mycompany.pkging.pkgingEJB.MyEJB would be the JNDI name for an EJB in the module pkgingEJB.jar, which is packaged in the pkging.ear application.
These elements are part of the glassfish-web.xml, glassfish-application-client.xml, glassfish-ejb-jar.xml, and glassfish-application.xml deployment descriptor files. For more information about how these elements behave in each of the deployment descriptor files, see Appendix C, Elements of the GlassFish Server Deployment Descriptors, in Oracle GlassFish Server 3.1 Application Deployment Guide.
The rest of this section uses an example of a JDBC resource lookup to describe how to reference resource factories. The same principle is applicable to all resources (such as JMS destinations, JavaMail sessions, and so on).
The @Resource annotation in the application code looks like this:
@Resource(name="jdbc/helloDbDs") javax.sql.DataSource ds;
This references a resource with the JNDI name of java:jdbc/helloDbDs. If this is the JNDI name of the JDBC resource configured in the GlassFish Server, the annotation alone is enough to reference the resource.
However, you can use a GlassFish Server specific deployment descriptor to override the annotation. For example, the resource-ref element in the glassfish-web.xml file maps the res-ref-name (the name specified in the annotation) to the JNDI name of another JDBC resource configured in the GlassFish Server.
<resource-ref> <res-ref-name>jdbc/helloDbDs</res-ref-name> <jndi-name>jdbc/helloDbDataSource</jndi-name> </resource-ref>