title: Glassfish: Unable to map datasource JNDI name to portable name using glassfish-web.xml
body: <p>I'm going slightly insane trying to make a Java EE 6 webapp portable between Glassfish AS 3.x and JBoss AS 6 (and 7 when released).</p>
  
  <p>Because each server maps JNDI names for datasources differently, I need to specify an application-private internal name for the datasource in persistence.xml then use glassfish-web.xml or jboss-web.xml (as appropriate) to map that to a real datasource name in the server.</p>
  
  <p>The theory is simple (well, for EE):</p>
  
  <ul>
  <li>Use internal name in persistence.xml, eg "my-datasource"</li>
  <li>Add a resource-ref entry to web.xml declaring that your app needs a resource called "my-datasource"</li>
  <li>Add a mapping in glassfish-web.xml and jboss-web.xml with the appropriate server's syntax, declaring that "my-datasource" should be mapped to the app server provided data source named "real-DS-created-by-admin"</li>
  </ul>
  
  <p>Unfortunately, the theory is about as far as it goes, because for the life of me I cannot make it work in Glassfish AS 3.1, 3.1.1, 3.2 beta, JBoss AS 6, or JBoss AS 7 beta. Right now I'm focusing on getting it working on Glassfish.</p>
  
  <p>Glassfish reports "Invalid resource : my-datasource__pm" when I try to deploy an app that references "my-datasource" in persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
    &lt;persistence-unit name="org.example_glassfish-webxml-datasource-jndi-mapping_war_1.0-SNAPSHOTPU" transaction-type="JTA"&gt;
      &lt;jta-data-source&gt;my-datasource&lt;/jta-data-source&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties/&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>and maps it to a known existing datasource via web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"     
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
  
      &lt;!-- servlet declarations etc elided ... --&gt; 
  
      &lt;resource-ref&gt;
          &lt;res-ref-name&gt;my-datasource&lt;/res-ref-name&gt;
          &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
          &lt;res-auth&gt;Container&lt;/res-auth&gt;
          &lt;res-sharing-scope&gt;Shareable&lt;/res-sharing-scope&gt;
      &lt;/resource-ref&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>... and glassfish-web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"&gt;
  &lt;glassfish-web-app error-url=""&gt;
    &lt;resource-ref&gt;
      &lt;res-ref-name&gt;my-datasource&lt;/res-ref-name&gt;
      &lt;jndi-name&gt;realdsname&lt;/jndi-name&gt;
    &lt;/resource-ref&gt;
  &lt;/glassfish-web-app&gt;
  </code></pre>
  
  <p>"asadmin list-jndi-entries" shows the actual datasource JNDI name exactly as it appears in glassfish-web.xml as well as listing another entry with a "__pm" suffix that's generated by Glassfish:</p>
  
  <pre><code>$ asadmin list-jndi-entries
  .... unrelated output ....
  realdsname__pm: javax.naming.Reference
  realdsname: javax.naming.Reference
  </code></pre>
  
  <p>Needless to say, this is driving me completely up the wall. Any ideas on what I'm missing?</p>