websphere - OpenLiberty NameNotFoundException : java:/comp/env

标签 websphere websphere-liberty open-liberty

我有一个在 Tomcat 中正确运行的 Web 应用程序,我想在新的 OpenLiberty 服务器上运行,该应用程序在 OpenLiberty 中正确启动,但在数据库连接启动时抛出以下异常:

[Default Executor-thread-15] 2018-03-15 15:02:30 ERROR TomcatConnectionManager:41 - Loading jdbc/mysql/myaap failure
javax.naming.NameNotFoundException: java:/comp/env
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLName.<init>(JavaURLName.java:83)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLNameParser.parse(JavaURLNameParser.java:39)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLNameParser.parse(JavaURLNameParser.java:60)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext$NameUtil.<init>(JavaURLContext.java:474)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:321)
    at com.ibm.ws.jndi.url.contexts.javacolon.internal.JavaURLContext.lookup(JavaURLContext.java:370)
    at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)

上面的异常是在查找阶段抛出的:

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");

有没有什么方法可以让它在 OpenLiberty 上工作,尽可能少做改动?

最佳答案

在 OpenLiberty 上,等效的查找看起来像这样:

    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup("java:comp/env");

关键是你需要使用java:comp/...而不是java:/comp/...


Tomcat 之所以不同于 Liberty,是因为 Tomcat 只是一个 servlet 容器,而 Liberty 符合完整的 Java EE 规范。

根据 EE.5.2.2 of the Java EE 7 spec 部分:

The application component’s naming environment is composed of four logical namespaces, representing naming environments with different scopes. The four namespaces are:

  • java:comp – Names in this namespace are per-component (for example, per enterprise bean). Except for components in a web module, each component gets its own java:comp namespace, not shared with any other component. Components in a web module do not have their own private component namespace. See note below.
  • java:module – Names in this namespace are shared by all components in a module (for example, all enterprise beans in a single EJB module, or all components in a web module).
  • java:app – Names in this namespace are shared by all components in all modules in a single application, where “single application” means a single deployment unit, such as a single ear file, a single module deployed standalone, etc. For example, a war file and an EJB jar file in the same ear file would both have access to resources in the java:app namespace.
  • java:global – Names in this namespace are shared by all applications deployed in an application server instance. Note that an application server instance may represent a single server, a cluster of servers, an administrative domain containing many servers, or even more. The scope of an application server instance is product-dependent, but it must be possible to deploy multiple applications to a single application server instance.

关于websphere - OpenLiberty NameNotFoundException : java:/comp/env,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49302912/

相关文章:

java - 使用 ServletOutputStream 在 Java servlet 中写入非常大的文件而不会出现内存问题

java - 使用反射来调用以 WebSphere 数据库连接作为参数的方法

jax-rs - 如何将专有的 Apache CXF 功能添加到 Open Liberty

java - 如何使用 Cargo 创建服务器并将 Web 应用程序部署到 WebSphere Liberty 中?

java - Open Liberty 上的 HTTP/2 支持

websphere-liberty - OpenLiberty 和 WebSphere Liberty 之间有什么区别?

Websphere 8.5 上下文根更改不适用于 war 文件

java - 如何在 RAD/Websphere 8.5 中进行热代码替换

websphere-liberty - 使用 microprofile-config-api : WELD-001408: Unsatisfied dependencies for type String with qualifiers @ConfigProperty

open-liberty - Kernel-slim UBI docker 镜像无法与 springBootUtility 一起使用