I am preparing a J2EE web application that should hopefully be packaged and sold to clients as a war file. Each client will deploy the package into his preferred application server and will use the app with his preferred Data Source.
I am wondering what’s the best portable way to define the necessary mappings to the Data Source.
The application uses the EclipseLink JPA Persistence Provider. The entry point is something like:
@PersistenceContext(unitName = "myPersistenceUnit")
private EntityManager em;
myPersistenceUnit is described in persistence.xml as follows:
<persistence-unit name="myPersistenceUnit" transaction-type="JTA">
<jta-data-source>myDataSource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
Now my guess is that I need to map myDataSource to the application server JNDI resource name somehow, but I am lost on how to do it. If I use the code auto-generated by NetBeans on GlassFish server, it creates the glassfish-resources.xml descriptor for the purpose. This approach works, but isn’t portable to other App Servers and produces a duplication of the Connection Pool’s properties (that are already defined in the Application Server).
Use relative JNDI mappings and link them in your web.xml.
You have to create a resource-ref entry in your web.xml.
Depending on application server the resource-ref entry in the web.xml will do. Others may require additional files.
Check the examples here for the web.xml part.
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/webapp/configureresources.html
http://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html