代码之家  ›  专栏  ›  技术社区  ›  Christopher Smith

在Tomcat中为Web应用配置JNDI JDBC

  •  0
  • Christopher Smith  · 技术社区  · 7 年前

    我无法找出JNDI JDBC数据源失败的原因

    上下文META-INF中的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>
    

    网状物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 回复  |  直到 7 年前
        1
  •  0
  •   Christopher Smith    7 年前

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