javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".

大致的需求是做一个定时器,定时做某件事,执行某段代码。但是因为工程的原因,使用的是hibernate调用数据库,并且配置的JNDI数据源,但是却没有JNDI的数据源权限,只有JNDI的名称。

定时器部分代码:

public void init() throws ServletException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(new Date()); Date d = null; try { d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date+" 01:00:00"); } catch (ParseException e) { e.printStackTrace(); } this.timer = new Timer("WeChatOnLineReportForms task1", true); this.timer.scheduleAtFixedRate(new TimerTask() { public void run() { System.out.println("-----WeChatOnLineReportForms task1 start !-----"); taskServiceImpl.tkCustomServiceTextTask1(); System.out.println("-----WeChatOnLineReportForms task1 End !-----");// System.out.println("88888888898"); } }, d, period); }

其中,taskServiceImpl.tkCustomServiceTextTask1(); 既要定时执行的程序,是从腾讯服务器拿到数据,然后批量插入数据库。

问题是:在执行定时器的时候,报出javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".这个错。

以下是详细的错误:

[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R javax.naming.ConfigurationException: A JNDI operation on a "java:" name cannot be completed because the server runtime is not able to associate the operation's thread with any J2EE application component.  This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request.  Make sure that a J2EE application does not execute JNDI operations on "java:" names within static code blocks or in threads created by that J2EE application.  Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on "java:" names. [Root exception is javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".]
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.ibm.ws.naming.java.javaURLContextImpl.throwConfigurationExceptionWithDefaultJavaNS(javaURLContextImpl.java:428)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.ibm.ws.naming.java.javaURLContextImpl.lookup(javaURLContextImpl.java:399)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:221)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:161)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at javax.naming.InitialContext.lookup(Unknown Source)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.zerowireinc.wechatonlinereportforms.util.JDBCUtil.createConnection(JDBCUtil.java:17)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.zerowireinc.wechatonlinereportforms.task.dao.TaskDaoImpl.saveCustomServiceText(TaskDaoImpl.java:22)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.zerowireinc.wechatonlinereportforms.task.service.TaskServiceImpl.tkCustomServiceTextTask1(TaskServiceImpl.java:80)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at com.zerowireinc.wechatonlinereportforms.task.action.Task1Servlet$1.run(Task1Servlet.java:53)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R at java.util.Timer$TimerImpl.run(Unknown Source)
[14-7-16 14:29:20:977 CST] 0000009b SystemErr     R Caused by: javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInternal(NameSpace.java:1837)
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1166)
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1095)
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(UrlContextImpl.java:1235)
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.ibm.ws.naming.java.javaURLContextImpl.lookup(javaURLContextImpl.java:395)
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R ... 8 more
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R java.lang.NullPointerException
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.zerowireinc.wechatonlinereportforms.task.dao.TaskDaoImpl.saveCustomServiceText(TaskDaoImpl.java:23)
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.zerowireinc.wechatonlinereportforms.task.service.TaskServiceImpl.tkCustomServiceTextTask1(TaskServiceImpl.java:80)
[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R at com.zerowireinc.wechatonlinereportforms.task.action.Task1Servlet$1.run(Task1Servlet.java:53)

[14-7-16 14:29:20:978 CST] 0000009b SystemErr     R 	at java.util.Timer$TimerImpl.run(Unknown Source)

字面的意思是找不到这个jndi,上网查证有说是数据源配置有问题,但是检查过数据源没有问题。

错误时在获取connection的时候读取数据源的时候抛出的,获取connection的代码为:

private static Connection conn = null;
private static InitialContext jndiCtx = null;


public static Connection createConnection() {
try {
jndiCtx = new InitialContext();
// DataSource ds = (DataSource) jndiCtx.lookup("java:comp/env/jdbc/InsureDB");
DataSource ds = (DataSource) jndiCtx.lookup("jdbc/InsureDB");
conn = ds.getConnection();
} catch (Exception e) {
System.out.println("Error getting JNDI context ");
e.printStackTrace();
} finally {

}
return conn;
}

而且奇怪的是在我本地测试时没有问题的,所以一开始的时候是怀疑是不是websphere服务器上的JNDI配置的问题,然后找公司的相关人员给查证了一下,服务器上的JNDI是没有问题的。然后一点点的试,输出日志。发现要定时执行的程序拉出来单独执行是没有任何问题的(就是把定时器去除,单独执行要定时执行的程序)。

所以又怀疑是否是定时器的问题。

偶尔从网上看到一篇JNDI的配置,jndiCtx.lookup("java:comp/env/jdbc/InsureDB");获取的时候lookup的是jndiCtx.lookup("jdbc/InsureDB");这种写法,然后试了一下。竟然成功了。

只能推断或许是跟JNDI的读取有关系。

但是因为定时器和JNDI之前都没用过,只是简单的了解过。所以具体的错误还没有查出来。所以说如果有大神知道是怎么回事,还请不吝赐教。

当然我也会在去查,查出来会跟帖的。


补充:

关于JNDI的一些知识:

百度百科JNDI 这个是百度百科给出的JNDI的示意,不过大致基本上看不懂,后来又

http://javacrazyer.iteye.com/blog/759485 这是某个大拿对JNDI的总结,受益匪浅。

http://yangzb.iteye.com/blog/242560  JNDI笔记

context.lookup("java:comp/env/XXX")和直接context.lookup("XXX")的区别 :http://f543711700.iteye.com/blog/1173618


你可能感兴趣的:(javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".)