Glassfish:无法使用glassfish-web.xml将数据源JNDI名称映射到可移植名称

Cra*_*ger 6 glassfish eclipselink java-ee-6

我试图让Glass EE AS 3.x和JBoss AS 6(以及发布后的7)之间的Java EE 6 webapp可移植,我有点疯狂.

因为每个服务器以不同方式映射数据源的JNDI名称,所以我需要在persistence.xml中为数据源指定应用程序专用内部名称,然后使用glassfish-web.xml或jboss-web.xml(根据需要)将其映射到实际服务器中的数据源名称.

理论很简单(对EE而言):

  • 在persistence.xml中使用内部名称,例如"my-datasource"
  • 向web.xml添加resource-ref条目,声明您的应用程序需要名为"my-datasource"的资源
  • 使用适当的服务器语法在glassfish-web.xml和jboss-web.xml中添加映射,声明应将"my-datasource"映射到名为"real-DS-created-by-admin"的应用服务器提供的数据源

不幸的是,这个理论就此而言,因为在我的生活中,我无法在Glassfish AS 3.1,3.1.1,3.2 beta,JBoss AS 6或JBoss AS 7 beta中使用它.现在我正专注于让它在Glassfish上运作.

当我尝试在persistence.xml中部署引用"my-datasource"的应用程序时,Glassfish报告"资源无效:my-datasource__pm":

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <persistence-unit name="org.example_glassfish-webxml-datasource-jndi-mapping_war_1.0-SNAPSHOTPU" transaction-type="JTA">
    <jta-data-source>my-datasource</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

并通过web.xml将其映射到已知的现有数据源:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!-- servlet declarations etc elided ... --> 

    <resource-ref>
        <res-ref-name>my-datasource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

</web-app>
Run Code Online (Sandbox Code Playgroud)

...和glassfish-web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!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">
<glassfish-web-app error-url="">
  <resource-ref>
    <res-ref-name>my-datasource</res-ref-name>
    <jndi-name>realdsname</jndi-name>
  </resource-ref>
</glassfish-web-app>
Run Code Online (Sandbox Code Playgroud)

"asadmin list-jndi-entries"显示实际数据源JNDI名称,与glassfish-web.xml中显示的名称完全相同,并列出另一个带有"__pm"后缀的条目,该条目由Glassfish生成:

$ asadmin list-jndi-entries
.... unrelated output ....
realdsname__pm: javax.naming.Reference
realdsname: javax.naming.Reference
Run Code Online (Sandbox Code Playgroud)

毋庸置疑,这让我完全崛起.关于我缺少的任何想法?

Cra*_*ger 9

好的,这是情况.

它不应该工作(见http://java.net/jira/browse/GLASSFISH-17024),显然没关系.

显然,每个人都在注释中定义他们的数据源,在web.xml <data-source />子句中,或者只针对一个app服务器.所有映射内容对JPA来说都是完全不起作用的,即使它适用于@Resource注入,JNDI查找,Spring等.

我已将此添加到我已经太长的java EE 6疣和陷阱页面中.