在 Tomcat 中为 Web 应用程序配置 JNDI JDBC

Configuring JNDI JDBC for Web App in Tomcat

我无法弄清楚为什么 JNDI JDBC 数据源失败

context.xml 在 META-INF

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
    <Resource auth="Container" 
        name="jdbc/BigByte" 
        type="javax.sql.DataSource"
        driverClassName="com.ibm.as400.access.AS400JDBCDriver"
        url="jdbc:as400://****.****/****;prompt=false;sort=language;sort language=ENU;sort weight=shared"
        username="****" 
        password="****" 
        maxIdle="10" 
        maxActive="200"
        maxWait="5" 
        removeAbandoned="true" 
        removeAbandonedTimeout="1200" />
</Context>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    *
    *
    *
    <resource-ref>
        <description>Big Byte DB Connection</description>
        <res-ref-name>jdbc/BigByte</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>

我的测试

    DataSource ds = null;
    try {
        Context initCtx = new InitialContext();
        NamingEnumeration<NameClassPair> list = initCtx.list("");
        while (list.hasMore()) {
            System.out.println(list.next().getName());
        }
        Context envCtx = (Context)initCtx.lookup("java:comp/env");
        ds = (DataSource)envCtx.lookup("jdbc/BigByte");
        Connection con = ds.getConnection();
        PreparedStatement ps = con.prepareStatement(
                "select * from XAAQREV1 where AQABVN = ?");
        ps.setString(1, "*****");
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            String uid = rs.getString("AQABVN");
            System.out.println(uid);
        }
    } catch (NamingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我的控制台输出

2017-09-28 10:13:26,482/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/naming/LocalStrings.class
2017-09-28 10:13:26,619/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/naming/LocalStrings_en.class
2017-09-28 10:13:26,775/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/naming/LocalStrings_en_US.class
2017-09-28 10:13:28,091/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/juli/JdkLoggerConfig.class
javax.naming.NameNotFoundException: Name [java:comp/env] is not bound in this Context. Unable to find [java:comp].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:824)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:172)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at webx0001.XAC3DFR_ObFnc.ObRun(XAC3DFR_ObFnc.java:189)
    ...

当我单步执行测试时,NamingEnumeration 列表没有任何元素。

有关丢失 类 的控制台消息是什么?

我错过了什么?

我没有遗漏任何东西。 我的项目使用了一个破坏 Tomcat JNDI 的自定义类加载器。 我在一个简单的项目中用一个简单的 servlet 验证了这一点。