尝试查找数据库时得到 javax.naming.NameNotFoundException
Getting javax.naming.NameNotFoundException when trying to look up database
我已经从 Eclipse 中开发了一个 REST Web 服务,目的是调用我们试图在 [=31] 中的独立 full.xml 文件中配置的数据库=] 我们正在使用的服务器。问题是,我们似乎错误地配置了数据库或错过了某种类型的步骤,所以我不确定我错过了哪一步来解决问题。
Web 服务调用此方法并在 getconnection 调用中失败:
public List<Object> getObjects(long id){
List<Objects> objects = new ArrayList<Objects>();
final StringBuffer query = //query removed here;
this.logger.info("Get Action: " + query.toString());
Statement ps = null;
Connection con = null;
ResultSet rs = null;
try {
con = this.getConnection("DBNAME2");
ps = con.createStatement();
SourceMaterial material = null;
rs = ps.executeQuery(query.toString());
while(rs.next()) {
//fields and object logic here
}
} catch (SQLException ex) {
this.logger.error(ex.getMessage());
} finally {
this.cleanUp(rs, ps, con);
}
return objects;
}
最终会遇到此代码并在 jndiName 查找时抛出异常:
protected void init(String dataSource) throws NamingException {
String jndiName = "";
if(dataSource.equals("DBNAME"))
jndiName = "java:jboss/datasources/jbpmDS";
else if(dataSource.equals("DBNAME2"))
jndiName = "java:jboss/datasources/ondisplayDS";
Hashtable<String, String> parms = new Hashtable<String, String>();
parms.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
//parms.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
InitialContext ctx = new InitialContext(parms);
this.datasource = (DataSource) ctx.lookup (jndiName);
}
JNDI 名称在我们的 standalone.xml 文件中配置如下:
<datasource jndi-name="java:jboss/datasources/ondisplayDS" pool-name="ondisplayDS" enabled="true" use-java-context="true">
<connection-url>jdbc:sqlserver://ServerNameHere:1931;databaseName=DataBaseNameHere;SendStringParametersAsUnicode=true;SelectMethod=cursor;user=GEN_DW_R;password=Re-KcU814RNWau</connection-url>
<driver>sql</driver>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
</pool>
</datasource>
抛出此堆栈跟踪:
09:22:22,190 ERROR [com.mmm.marketing.dao.utils.DatasourceServiceLocator] (http-localhost/127.0.0.1:8080-1) datasources/ondisplayDS -- service jboss.naming.context.java.jboss.datasources.ondisplayDS: javax.naming.NameNotFoundException: datasources/ondisplayDS -- service jboss.naming.context.java.jboss.datasources.ondisplayDS
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:103)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:197)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:174)
at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:243)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:183)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179)
at javax.naming.InitialContext.lookup(InitialContext.java:411) [rt.jar:1.7.0_71]
at javax.naming.InitialContext.lookup(InitialContext.java:411) [rt.jar:1.7.0_71]
at com.mmm.marketing.dao.utils.DatasourceServiceLocator.init(DatasourceServiceLocator.java:83) [classes:]
at com.mmm.marketing.dao.utils.DatasourceServiceLocator.<init>(DatasourceServiceLocator.java:149) [classes:]
at com.mmm.marketing.dao.utils.DatasourceServiceLocator.getInstance(DatasourceServiceLocator.java:110) [classes:]
at com.mmm.marketing.dao.DAO.getConnection(DAO.java:40) [classes:]
at com.mmm.marketing.dao.MaterialDAO.getMaterials(MaterialDAO.java:30) [classes:]
at com.mmm.marketing.MaterialResource.material_list(MaterialResource.java:35) [classes:]
at com.mmm.marketing.MaterialResource$Proxy$_$$_WeldClientProxy.material_list(MaterialResource$Proxy$_$$_WeldClientProxy.java) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_71]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_71]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_71]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_71]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:395) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_71]
同样,我假设有一个简单的修复方法。我唯一的问题是我实际上并不知道从 JBoss 中配置我们的数据库的正确过程,我真的只是在猜测。
有没有人有任何见解或可以指出我缺少配置步骤的过程方向?
我最终进入我的服务器的管理控制台并添加我在独立文件中配置的数据源,代码现在可以工作了。
我已经从 Eclipse 中开发了一个 REST Web 服务,目的是调用我们试图在 [=31] 中的独立 full.xml 文件中配置的数据库=] 我们正在使用的服务器。问题是,我们似乎错误地配置了数据库或错过了某种类型的步骤,所以我不确定我错过了哪一步来解决问题。
Web 服务调用此方法并在 getconnection 调用中失败:
public List<Object> getObjects(long id){
List<Objects> objects = new ArrayList<Objects>();
final StringBuffer query = //query removed here;
this.logger.info("Get Action: " + query.toString());
Statement ps = null;
Connection con = null;
ResultSet rs = null;
try {
con = this.getConnection("DBNAME2");
ps = con.createStatement();
SourceMaterial material = null;
rs = ps.executeQuery(query.toString());
while(rs.next()) {
//fields and object logic here
}
} catch (SQLException ex) {
this.logger.error(ex.getMessage());
} finally {
this.cleanUp(rs, ps, con);
}
return objects;
}
最终会遇到此代码并在 jndiName 查找时抛出异常:
protected void init(String dataSource) throws NamingException {
String jndiName = "";
if(dataSource.equals("DBNAME"))
jndiName = "java:jboss/datasources/jbpmDS";
else if(dataSource.equals("DBNAME2"))
jndiName = "java:jboss/datasources/ondisplayDS";
Hashtable<String, String> parms = new Hashtable<String, String>();
parms.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
//parms.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
InitialContext ctx = new InitialContext(parms);
this.datasource = (DataSource) ctx.lookup (jndiName);
}
JNDI 名称在我们的 standalone.xml 文件中配置如下:
<datasource jndi-name="java:jboss/datasources/ondisplayDS" pool-name="ondisplayDS" enabled="true" use-java-context="true">
<connection-url>jdbc:sqlserver://ServerNameHere:1931;databaseName=DataBaseNameHere;SendStringParametersAsUnicode=true;SelectMethod=cursor;user=GEN_DW_R;password=Re-KcU814RNWau</connection-url>
<driver>sql</driver>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
</pool>
</datasource>
抛出此堆栈跟踪:
09:22:22,190 ERROR [com.mmm.marketing.dao.utils.DatasourceServiceLocator] (http-localhost/127.0.0.1:8080-1) datasources/ondisplayDS -- service jboss.naming.context.java.jboss.datasources.ondisplayDS: javax.naming.NameNotFoundException: datasources/ondisplayDS -- service jboss.naming.context.java.jboss.datasources.ondisplayDS
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:103)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:197)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:174)
at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:243)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:183)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:179)
at javax.naming.InitialContext.lookup(InitialContext.java:411) [rt.jar:1.7.0_71]
at javax.naming.InitialContext.lookup(InitialContext.java:411) [rt.jar:1.7.0_71]
at com.mmm.marketing.dao.utils.DatasourceServiceLocator.init(DatasourceServiceLocator.java:83) [classes:]
at com.mmm.marketing.dao.utils.DatasourceServiceLocator.<init>(DatasourceServiceLocator.java:149) [classes:]
at com.mmm.marketing.dao.utils.DatasourceServiceLocator.getInstance(DatasourceServiceLocator.java:110) [classes:]
at com.mmm.marketing.dao.DAO.getConnection(DAO.java:40) [classes:]
at com.mmm.marketing.dao.MaterialDAO.getMaterials(MaterialDAO.java:30) [classes:]
at com.mmm.marketing.MaterialResource.material_list(MaterialResource.java:35) [classes:]
at com.mmm.marketing.MaterialResource$Proxy$_$$_WeldClientProxy.material_list(MaterialResource$Proxy$_$$_WeldClientProxy.java) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_71]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_71]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_71]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_71]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.authenticator.SingleSignOn.invoke(SingleSignOn.java:395) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_71]
同样,我假设有一个简单的修复方法。我唯一的问题是我实际上并不知道从 JBoss 中配置我们的数据库的正确过程,我真的只是在猜测。
有没有人有任何见解或可以指出我缺少配置步骤的过程方向?
我最终进入我的服务器的管理控制台并添加我在独立文件中配置的数据源,代码现在可以工作了。