尝试查找数据库时获取javax.naming.NameNotFoundException

时间:2015-02-20 14:41:01

标签: java eclipse jboss

我从Eclipse开发了一个REST Web服务,其目标是调用我们尝试在我们正在使用的JBoss服务器中的standalone-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配置数据库的正确程序,我真的只是在猜测。

有没有人有任何见解或者可以指出我在缺少配置步骤的程序方向?

1 个答案:

答案 0 :(得分:0)

我最终进入服务器的管理控制台并添加了我在独立文件中配置的数据源,现在代码正常工作。