javax.naming.NameNotFoundException:名称[comp / env]未绑定在此Context中。无法使用java调度程序找到[comp]错误

问题描述 投票:7回答:5

我想要做的是在一段时间后更新我的数据库。所以我正在使用java调度程序和连接池。我不知道为什么,但我的代码只工作一次。它将打印:

init success
success
javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at test.Pool.main(Pool.java:25)  ---> line 25 is Context envContext = (Context)initialContext.lookup("java:/comp/env");

我不知道为什么它只能运作一次。如果没有java调度程序我没有运行它我已经测试了它并且它工作正常。没有错误,无论如何。如果我使用调度程序运行它,不知道为什么我会收到此错误。

希望可以有人帮帮我。

我的连接池代码:

public class Pool {
public DataSource main() {
    try {
        InitialContext initialContext = new InitialContext();
        Context envContext = (Context)initialContext.lookup("java:/comp/env");
        DataSource datasource = new DataSource();   
        datasource = (DataSource)envContext.lookup("jdbc/test");
        return datasource;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

}

我的web.xml:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
    <listener-class> package.test.Pool</listener-class>
</listener>
<resource-ref>
    <description>DB Connection Pooling</description>
    <res-ref-name>jdbc/test</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

context.xml中:

    <?xml version="1.0" encoding="UTF-8"?>
<Context path="/project" reloadable="true">
  <Resource auth="Container" 
            defaultReadOnly="false" 
            driverClassName="com.mysql.jdbc.Driver" 
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 
            initialSize="0" 
            jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" 
            jmxEnabled="true" 
            logAbandoned="true" 
            maxActive="300" maxIdle="50"
            maxWait="10000" 
            minEvictableIdleTimeMillis="300000"
            minIdle="30" 
            name="jdbc/test"
            password="test" 
            removeAbandoned="true"
            removeAbandonedTimeout="60" 
            testOnBorrow="true"
            testOnReturn="false" 
            testWhileIdle="true" 
            timeBetweenEvictionRunsMillis="30000"
            type="javax.sql.DataSource" 
            url="jdbc:mysql://localhost:3306/database?noAccessToProcedureBodies=true" 
            username="root"
            validationInterval="30000" 
            validationQuery="SELECT 1"/>
</Context>

我的java调度程序

public class Scheduler extends HttpServlet{

public void init() throws ServletException
{
    System.out.println("init success");
    try{
        Scheduling_test test = new Scheduling_test();
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(100);
        ScheduledFuture future = executor.scheduleWithFixedDelay(test, 1, 60 ,TimeUnit.SECONDS);
    }catch(Exception e){
         e.printStackTrace();
    }
}

}

Schedule_test

    public class Scheduling_test extends Thread implements Runnable{
    public void run(){
        Updating updating = new Updating();
        updating.run();
    }
}

更新

public class Updating{

        public void run(){
            ResultSet rs = null;
            PreparedStatement  p = null;
            StringBuilder sb = new StringBuilder();
            Pool pool = new Pool();
            Connection con = null; 
            DataSource datasource = null;
            try{
                datasource = pool.main();
                con=datasource.getConnection();
                sb.append("SELECT * FROM database");
                p = con.prepareStatement(sb.toString());
                rs = p.executeQuery();
                rs.close();
                con.close();
                p.close();
                datasource.close();
                System.out.println("success");
         }catch (Exception e){
                e.printStackTrace();
         }
    }
java sql scheduled-tasks connection-pooling runtime-error
5个回答
3
投票

此错误表明您的jdbc资源未注册!你把context.xml放在哪里?

context.xml文件必须位于war文件的META-INF目录中。它不能位于classes目录或jar文件中。

META-INF目录与context.xml放在源文件夹树中包含webapp根目录的目录中。


1
投票

我有同样的问题拖延了两个痛苦的日子。我重新阅读关于JNDI的tomcat文档,确认我所有的server.xmlcontext.xmlweb.xml配置和访问命名对象的代码......仍然没有进展。

事实证明,这是我项目中的依赖项,它干扰了命名上下文 - axis2。从axis2-1.6.0到axis2-1.6.2的简单移动引起了我的问题。


0
投票

尝试停止并重新启动服务器(我假设您使用的是Eclipse和Tomcat服务器)。也许当您创建上下文xml文件时,它最初是空的,您的服务器无法同步其内容,因此无法找到您稍后在context.xml文件中指定的数据库连接资源名称。

即如果你还没有解决这个问题。


0
投票

这不是java:/comp/env而是java:comp/env


0
投票

我刚刚遇到同样的问题。在我的情况下,我的tomcat配置文件或我的项目没有任何问题,但我必须重启我的postgres服务实例,因为我的tomcat正在运行,它会以某种方式获取更改,异常消失。我不得不重新启动窗口,让问题完全消失。不明白为什么!

© www.soinside.com 2019 - 2024. All rights reserved.