Skip to main content

Indirection of persistence.xml datasource via web.xml and glassfish-web.xml: how?

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
ringerc
Offline
Joined: 2008-06-08

Hi all

I've been struggling for days to make my app portable between Glassfish 3.1.1 and JBoss AS 7. The main issue I'm encountering is with JNDI naming of datasources.

The "standard" approach to app-server differences in JNDI naming is to map an application-local name to a container-provided name by adding a resource-ref in web.xml, then mapping it to a container jndi name using glassfish-web.xml / jboss-web.xml . I've been completely unable to get this to work with either Glassfish or JBoss, both of which seem to completely ignore the indirection and try to look the app-local name up directly in the container JNDI namespace.

I've just been informed that this is exactly how it's supposed to work:

"The resource-ref element that you refer to above defines an indirection within a component name space. A PersistenceUnit (EMF) is not initialized within a component and hence always uses the global name space to look up data sources."

This leads me to the question: how on earth are you supposed to avoid hard-coding an app-server-specific data source name in persistence.xml ? Isn't that what resource-ref and server-specific deployment descriptor indirection is for? Isn't it bizarre to deny the "new standard" method of data access, JPA 2.0, use of the same mapping system and rules already available to apps that use JDBC directly?

How do I work around this? Make app-server-specific overlays for my WAR files that add a different persistence.xml for each server? That just can't be right.