java 为什么这里会出现 javax.naming.NamingException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10369867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:44:27  来源:igfitidea点击:

why does the javax.naming.NamingException occur here?

javajdbcnaming

提问by Suhail Gupta

when i run the following :

当我运行以下命令时:

package NonServletFiles;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import javax.naming.*;

public class GetTagsFromDatabase {

public GetTagsFromDatabase() {

}

public String[] getTags() {

    String tags[] = null;
    try {
        Context context = new InitialContext();
        DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/photog"); // <<----- line 23
        Connection connection = ds.getConnection();
        String sqlQuery = "select NAMEOFTHETAG from tagcollection";
        PreparedStatement statement = connection.prepareStatement(sqlQuery);
        ResultSet set = statement.executeQuery();

        int i = 0;
        while(set.next()) {
            tags[i] = set.getString("NameOfTheTag");
            System.out.println(tags[i]);
            i++;
        }
    }catch(Exception exc) {
        exc.printStackTrace();
    }

    return tags;
}

public static void main(String args[]) {
    new GetTagsFromDatabase().getTags(); // <<----- line 43
}
}

I get the following exceptions :

我得到以下例外:

javax.naming.NamingException: Lookup failed for 'java:comp/env/jdbc/photog' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at NonServletFiles.GetTagsFromDatabase.getTags(GetTagsFromDatabase.java:23)
at NonServletFiles.GetTagsFromDatabase.main(GetTagsFromDatabase.java:43)

Caused by: javax.naming.NamingException: Invocation exception: Got null ComponentInvocation 
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.getComponentId(GlassfishNamingManagerImpl.java:873)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:742)
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:172)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
... 4 more

I don't know the reason for this exception,all other servlets that need to connect to the database with the url java:comp/env/jdbc/photogwork fine.

我不知道此异常的原因,所有其他需要使用 url 连接到数据库的 servlet 都可以java:comp/env/jdbc/photog正常工作。

回答by BalusC

The stacktrace hints that you're using Glassfish. Remove the java:comp/env/part. It's the default JNDI context root already. Only in Tomcat you need to specify it explicitly. Also, you should be invoking this in webapp context, not as a plain Java Application with main().

堆栈跟踪提示您正在使用 Glassfish。取下java:comp/env/零件。它已经是默认的 JNDI 上下文根。仅在 Tomcat 中您需要明确指定它。此外,您应该在 webapp 上下文中调用它,而不是作为带有main().



Unrelatedto the concrete problem, do you really need to get the DataSourceeverytime? I'd create a helper class which obtains it only once on webapp's startup or in a static initializer. It's application wide and threadsafe. Only the Connectionindeed needs to be obtained (and closed! you're not closing it, so you're leaking DB resources) everytime you need to fire a SQL query.

具体问题无关,你真的需要得到DataSourceeverytime吗?我会创建一个助手类,它只在 webapp 启动时或在静态初始化程序中获取一次。它是应用程序范围和线程安全的。Connection每次需要触发 SQL 查询时,只需要获取(并关闭!你没有关闭它,所以你正在泄漏数据库资源)。