【问题标题】:InitialContext factory errorInitialContext 工厂错误
【发布时间】:2013-06-17 14:49:13
【问题描述】:

我正在研究 IBM 发布的Managing database connections with JDBC。这是一些旧东西(2001)。他们正在使用 JNDI。当我尝试实现他们的代码时:

 try {
        Hashtable env = new Hashtable();
        env.put(
        Context.INITIAL_CONTEXT_FACTORY,
        "com.sun.jndi.fscontext.RefFSContextFactory");

        // Create the initial context

        Context ctx = new InitialContext(env);

        // Here we create the actual DataSource and then set the relevant 
        // parameters.

        TdsDataSource ds = new TdsDataSource();

        ds.setServerName(serverName);
        ds.setPortNumber(portNumber);
        ds.setDatabaseName(databaseName);
        ds.setUser(login);
        ds.setPassword(password);
        ds.setDescription("JDBC DataSource Connection");

        // Now we bind the DataSource object to the name we selected earlier.

        ctx.bind(filePath, ds);
        ctx.close();

    // Generic Exception handler, in practice, this would be replaced by an 
    // appropriate Exception handling hierarchy.

    } catch (Exception ex) {
        System.err.println("ERROR: " + ex.getMessage());
    }

但是我发现没有“com.sun.jndi.fscontext.RefFSContextFactory”文件系统服务提供者。然后我将代码更改如下(来自Initialize Data Source Properties)。

OracleDataSource ods = new OracleDataSource();
ods.setDriverType("oci");
ods.setServerName("dlsun999");
ods.setNetworkProtocol("tcp");
ods.setDatabaseName("816");
ods.setPortNumber(1521);
ods.setUser("scott");
ods.setPassword("tiger");
Context ctx = new InitialContext();
ctx.bind("jdbc/sampledb", ods);

当我尝试执行此代码时,我收到以下错误:

ERROR: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

我认为它仍在要求 Context.INITIAL_CONTEXT_FACTORY。有什么解决办法吗?我从早上开始就在寻找它。

【问题讨论】:

    标签: java oracle datasource jndi


    【解决方案1】:

    在创建InitialContext 对象之前尝试包含以下行。它应该可以解决问题。

    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");  
    

    这基本上告诉System 您正在使用哪个初始上下文库来存储数据源上下文。

    【讨论】:

    • 那也行不通。这是说 jdbc(在“jdbc/sampledb”中)不在上下文中。
    【解决方案2】:

    如果您可以发布 StactTrace 或有关您正在运行此代码的容器/服务器的任何详细信息,将会很有帮助。

    DataSource 对象和 JNDI 上下文是两种不同的资源,尽管它们是一起使用的。 JNDI 是一个存储库,可以保存由名称等路径标识的对象。 DataSource 是一个对象,其中包含使用 JDBC 建立数据库连接的信息。为了提供对 DataSource 的系统范围的访问,它的对象将使用一个键绑定到 JNDI,以便可以在系统内全局查找它。

    您的问题似乎与 JNDI 提供程序库有关。您无需为此更改 DataSource 实现库(从 TdsDataSourceOracleDataSource)。

    也许这个细节可以帮助您或发布更多细节以了解您的运行时环境。

    【讨论】:

      猜你喜欢
      • 2012-10-18
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      相关资源
      最近更新 更多