在Tomcat中为Web App配置JNDI JDBC

时间:2017-09-28 14:23:57

标签: jdbc jndi tomcat8

我无法弄清楚JNDI JDBC数据源失败的原因

META-INF

中的

context.xml

<?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列表没有元素。

关于缺少类的控制台消息是什么?

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我没有遗漏任何东西。 我的项目使用了一个破坏Tomcat JNDI的Custom ClassLoader。 我在一个简单的项目中用一个简单的servlet验证了这一点。