在linux上,日志经常打印一个错误:
Initial Context fail, result:Name java: is not bound in this Context
java.lang.RuntimeException: java.lang.NullPointerException
at com.hk_life.util.DataBaseUtil.getConnection(DataBaseUtil.java:38)
at com.hk_life.util.JdbcTemplate.query(JdbcTemplate.java:171)
at com.hk_life.util.JdbcTemplate.query(JdbcTemplate.java:65)
at com.hk_life.dao.ProjectNodeDao.queryAllNode(ProjectNodeDao.java:13)
at com.hk_life.task.JdAssetSyncThreadTask.execute(JdAssetSyncThreadTask.java:38)
at com.hk_life.task.CommonTask.run(CommonTask.java:14)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Caused by: java.lang.NullPointerException
对应的代码段:
public Connection getConnection() throws SQLException, NamingException {
// 初始化查找命名空间
if (getDataSource.envContext == null) {
try {
Context initContext = new InitialContext();
getDataSource.envContext = (Context) initContext
.lookup("java:/comp/env");
} catch (Exception e) {
System.out.println("Initial Context fail, result:"
+ e.getMessage());
}
}
// 找到DataSource
DataSource ds = (DataSource) getDataSource.envContext
.lookup("jdbc/dbconnectionpool");
Connection c = ds.getConnection();
//Connection c=DataBaseUtil.getConnection();
return c;
}
经查询,需要将"java:/comp/env"改为"java:comp/env" ,即去掉前面的斜杠“/”