新起线程获取不到JNDI
今天在部署JBOSS之后进行测试,结果报了下面的错误:
javax.naming.NameNotFoundException: java:comp/env/jdbc/jndiName
2018-09-04 11:03:13,463 ERROR [stderr] (Thread-114083) at org.jboss.as.naming.InitialContext$DefaultInitialContext.findContext(InitialContext.java:189)
2018-09-04 11:03:13,463 ERROR [stderr] (Thread-114083) at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:233)
2018-09-04 11:03:13,463 ERROR [stderr] (Thread-114083) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
2018-09-04 11:03:13,463 ERROR [stderr] (Thread-114083) at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
2018-09-04 11:03:13,463 ERROR [stderr] (Thread-114083) at javax.naming.InitialContext.lookup(InitialContext.java:417)
2018-09-04 11:03:13,463 ERROR [stderr] (Thread-114083) at javax.naming.InitialContext.lookup(InitialContext.java:417)
想了半天,关于JNDI,什么也没改啊,奇了怪了,昨天还好好的,怎么突然间就找不到 JNDI name了呢?
下面的兄台也遇到同样的问题,看了一遍,大概了解了,是因为记录Log 是自己单独起了线程去完成的。
https://developer.jboss.org/thread/173179
http://lists.jboss.org/pipermail/jboss-as7-dev/2011-June/002292.html
注意到上面打印出来的日志,线程名字都是Thread-xxx, 表明是non EE-threads, 即用户自己创建的,因为如果是servlet or EJB, 会显示:default task-xx
第二位仁兄基本上了把问题解释清楚了:
The "comp" namespace is so-named because it corresponds to a
"component", such as an EJB or servlet (though due to historical quirks,
"comp" is shared among all servlets in a WAR). If you're not in a
context associated with a component, then the AS cannot know which
component's "comp" namespace to examine.
翻译过来大概意思就是,我们在 JNDI 设置中使用java:comp这个命名空间是因为它是一个组件,如 EJB 或者 servlet(尽管由于历史巧合,“comp” 由war包中的所有serlvet 共享).所以如果你不是从跟组件相关的context 会话创建的线程, AS 服务器就不知道该用哪个组件的“comp”, 那就直接报错,说我找不到这个JNDI 了。