2017-05-23 39 views
0

所以我最近开始在一个新的项目中工作。这些家伙有这样的申报数据源连接:加载数据源时出错

Context init = new InitialContext(); 
Context context = (Context) init.lookup("java:comp/env"); 
ds = (DataSource)context.lookup("jdbc/WhateverDS"); 

我不知道他们为什么这样宣布它。我一直看到它是这样的:

DataSource ds = new InitialContext().lookup("java:comp/env/jdbc/WhateverDS"); 

首先,你们知道他们为什么要分两步做吗?

我的第二个问题是,有时我们得到以下错误,如果它可能是与上面的代码:

Caused by: javax.naming.NameNotFoundException: The name comp/env is not bound in this context 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:818) 
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166) 
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157) 
    at javax.naming.InitialContext.lookup(InitialContext.java:411) 
    ... 19 more 

我们使用JDBC使用MySQL而不是Tomcat。

编辑: 他们没有任何关于在web.xml中声明的数据库。 context.xml中(其它参数中)看起来是这样的:

Resource driverClassName="com.mysql.jdbc.Driver" 
      name="jdbc/WhateverDS" 
      type="javax.sql.DataSource" 
      url="jdbc:mysql://localhost:3306/someDB" 
+0

重复好像是https://stackoverflow.com/questions/21527714/javax-naming-namenotfoundexception-name-comp-env-is-not的副本-bound-in-this-context – Koshux

+0

[javax.naming.NameNotFoundException:Name \ [comp/env \]在此上下文中未绑定的可能重复](https://stackoverflow.com/questions/21527714/javax-naming -namenotfoundexception-name-comp-env-is-not-bound-in-this-context) – Koshux

+0

你在web.xml中定义了这个配置吗? – Thiagz

回答