Glassfish:无法使用 glassfish-web.xml 将数据源 JNDI 名称映射到可移植名称
我试图让 Java EE 6 Web 应用程序在 Glassfish AS 3.x 和 JBoss AS 6(以及发布后的 7)之间移植,这有点疯狂。
由于每个服务器为数据源映射 JNDI 名称的方式不同,因此我需要在 persistence.xml 中为数据源指定应用程序私有的内部名称,然后使用 glassfish-web.xml 或 jboss-web.xml (视情况而定)将其映射到真实的数据源。服务器中的数据源名称。
理论很简单(对于 EE):
- 在 persistence.xml 中使用内部名称,例如“my-datasource”
- 在 web.xml 中添加一个资源引用条目,声明您的应用程序需要一个名为“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>
并通过 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>
...和 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>
"asadmin list-jndi-entries" 显示实际的数据源 JNDI 名称,与 glassfish-web.xml 中显示的完全相同,并列出由 Glassfish 生成的带有“__pm”后缀的另一个条目:
$ asadmin list-jndi-entries
.... unrelated output ....
realdsname__pm: javax.naming.Reference
realdsname: javax.naming.Reference
不用说,这让我完全陷入困境。对我所缺少的有什么想法吗?
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).
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.
The theory is simple (well, for EE):
- Use internal name in persistence.xml, eg "my-datasource"
- Add a resource-ref entry to web.xml declaring that your app needs a resource called "my-datasource"
- 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"
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.
Glassfish reports "Invalid resource : my-datasource__pm" when I try to deploy an app that references "my-datasource" in persistence.xml:
<?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>
and maps it to a known existing datasource via 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>
... and 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>
"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:
$ asadmin list-jndi-entries
.... unrelated output ....
realdsname__pm: javax.naming.Reference
realdsname: javax.naming.Reference
Needless to say, this is driving me completely up the wall. Any ideas on what I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,情况是这样的。
它不应该工作(请参阅 http://java.net/jira/browse/GLASSFISH-17024< /a>) 显然没问题。
显然每个人都在注释中定义他们的数据源,在 web.xml中子句,或仅针对一台应用程序服务器。所有映射内容对于 JPA 来说都是完全不起作用的,尽管它对于 @Resource 注入、JNDI 查找、Spring 等工作得很好。
我已经将其添加到我已经太长的 java EE 6 的缺点和陷阱页面。
OK, here's the situation.
It's not supposed to work (see http://java.net/jira/browse/GLASSFISH-17024) and apparently that's OK.
Apparently everybody defines their data sources in annotations, in web.xml <data-source/> clauses, or just targets only one app server. All the mapping stuff is completely non-functional for JPA even though it works fine for @Resource injection, JNDI lookups, Spring, etc.
I've added this to my already-way-too-long java EE 6 warts and traps page.