【问题标题】:JNDI Context Initialization error while migration from WAS to liberty从 WAS 迁移到 Liberty 时出现 JNDI 上下文初始化错误
【发布时间】:2021-08-03 18:19:51
【问题描述】:

WsnInitialContextFactory 类未找到异常。 Liberty 不支持这个类,那我怎么去掉jndi.properties 默认设置。此属性文件包含 InitialContext 的此类名称。

属性:

INITIAL_CONTEXT_FACTORY = "com.ibm.websphere.naming.WsnInitialContextFactory" 
CC_PROVIDER_URL = "iiop://localhost.svr.us.xxxxxxx.net:41516"

InitialContext 查找

   try {
        Hashtable params = new Hashtable();
        params.put(Context.INITIAL_CONTEXT_FACTORY, ADProperties.WS_INITIAL_CONTEXT_FACTORY);
        params.put(Context.PROVIDER_URL, ADProperties.AD_PROVIDER_URL);

        Context ctx = new InitialContext(params);
        try {
            ds = (DataSource) ctx.lookup("java:comp/env/" + jndiName);
        } catch (Exception ex) {
            LogUtil.debugException("DBConnectionManager", "Exception in init: Resource reference DataSource lookup Error.", "init()", ex, "");
            try {
                ds = (DataSource) ctx.lookup("jdbc/" + jndiName);
            } catch (Exception exp) {
                LogUtil.debugException("DBConnectionManager", "Exception in init: Datasource lookup Error.", "init()", exp, "");
            }
        }

例外:

[INFO] [err] java.lang.NoClassDefFoundError: Could not initialize class com.ibm.websphere.naming.WsnInitialContextFactory
[INFO] [err]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[INFO] [err]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
[INFO] [err]    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[INFO] [err]    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
[INFO] [err]    at com.ibm.ws.jndi.internal.WASInitialContextFactoryBuilder.createInitialContextFactory(WASInitialContextFactoryBuilder.java:57)
[INFO] [err]    at [internal classes]
[INFO] [err]    at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:730)
[INFO] [err]    at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
[INFO] [err]    at java.naming/javax.naming.InitialContext.init(InitialContext.java:236)
[INFO] [err]    at java.naming/javax.naming.InitialContext.<init>(InitialContext.java:184)

【问题讨论】:

  • 您必须切换到默认构造函数 new InitialContext(),正如 Alasdair 所写,确保您的应用程序或类路径中没有 jndi.properties。此代码在客户端应用程序中??您需要编写更多详细信息,您的应用程序是如何构建的,以及它是如何使用这部分代码的。

标签: jms jndi open-liberty


【解决方案1】:

Liberty 不支持 WsnInitialContextFactory 类。您可以这样做(它也可以在传统的 WebSphere 中工作):

try {
    Context ctx = new InitialContext(new Hashtable());
    try {
        ds = (DataSource) ctx.lookup("java:comp/env/" + jndiName);
    } catch (Exception ex) {
        LogUtil.debugException("DBConnectionManager", "Exception in init: Resource reference DataSource lookup Error.", "init()", ex, "");
        try {
            ds = (DataSource) ctx.lookup("jdbc/" + jndiName);
        } catch (Exception exp) {
            LogUtil.debugException("DBConnectionManager", "Exception in init: Datasource lookup Error.", "init()", exp, "");
        }
    }

【讨论】:

  • 它不能解决问题。我仍然遇到同样的错误。
  • @Gas - 你能在这里看看这个问题吗?
  • 类路径中有 jndi.properties 文件吗?是否设置了指定 InitialContextFactory 配置的系统属性?
  • 我没有 jndi.properties。但是我已经阅读了即使您没有在初始上下文中提供属性的详细信息。它将从 jndi.properties 中获取此类。
  • 您是否建议任何其他替代类作为初始上下文工厂?
猜你喜欢
  • 2018-11-13
  • 2015-10-13
  • 2011-08-06
  • 1970-01-01
  • 2018-09-28
  • 2014-10-27
  • 2019-05-15
  • 1970-01-01
  • 2019-07-02
相关资源
最近更新 更多