简体   繁体   中英

javax.naming.NameNotFoundException in eclipse while using JNDI to connect to datasource created in JBOSS 7

I am new to J2EE. I am trying to connect to datasource created in jboss AS 7 using JNDI.

I am using Jboss AS 7 Standalone server to deploy my project NewDB.war . I have created fallowing data source java:jboss/datasources/oracleDS (which is my JNDI name) in Jboss AS 7 and successfully connected to Oracle database.

I am using oracle database to store my sql tables.

I am getting fallowing error message in eclipse. but i got success message in command propmpt. I created Dynamic web project in eclipse for performing all these tasks.I am not sure which type of project to create for my task. Correct me if i am wrong.

It will be great help if anyone helps getting out of this error.

        09:53:18,418 INFO  [org.jboss.modules] JBoss Modules version 1.1.1.GA
    09:53:19,421 INFO  [org.jboss.msc] JBoss MSC version 1.0.2.GA
    09:53:19,495 INFO  [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting
    09:53:22,118 INFO  [org.xnio] XNIO Version 3.0.3.GA
    09:53:22,118 INFO  [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
    09:53:22,122 INFO  [org.xnio.nio] XNIO NIO Implementation Version 3.0.3.GA
    09:53:22,137 INFO  [org.jboss.remoting] JBoss Remoting version 3.2.3.GA
    09:53:22,184 INFO  [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
    09:53:22,200 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 31) JBAS010280: Activating Infinispan subsystem.
    09:53:22,215 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 38) JBAS011800: Activating Naming Subsystem
    09:53:22,200 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
    09:53:22,200 INFO  [org.jboss.as.osgi] (ServerService Thread Pool -- 39) JBAS011940: Activating OSGi Subsystem
    09:53:22,200 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 44) JBAS013101: Activating Security Subsystem
    09:53:22,200 INFO  [org.jboss.as.configadmin] (ServerService Thread Pool -- 26) JBAS016200: Activating ConfigAdmin Subsystem
    09:53:22,340 INFO  [org.jboss.as.naming] (MSC service thread 1-7) JBAS011802: Starting Naming Service
    09:53:22,356 INFO  [org.jboss.as.security] (MSC service thread 1-1) JBAS013100: Current PicketBox version=4.0.7.Final
    09:53:22,403 INFO  [org.jboss.as.connector] (MSC service thread 1-7) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.9.Final)
    09:53:22,465 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-2) JBAS015400: Bound mail session [java:jboss/mail/Default]
    09:53:22,559 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
    09:53:23,394 INFO  [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-8) Starting Coyote HTTP/1.1 on http-localhost-127.0.0.1-8080
    09:53:23,564 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
    09:53:23,642 INFO  [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-5) JBoss Web Services - Stack CXF Server 4.0.2.GA
    09:53:24,126 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-3) JBAS015012: Started FileSystemDeploymentService for directory C:\Users\patillat\jboss-as-7.1.1.Final\standalone\deployments
    09:53:24,142 WARN  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015002: Deployment of 'NewDBEAR.ear' requested, but the deployment is not present
    09:53:24,158 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found NewDB.war in deployment directory. To trigger deployment create a file called NewDB.war.dodeploy
    09:53:24,173 INFO  [org.jboss.as.remoting] (MSC service thread 1-1) JBAS017100: Listening on localhost/127.0.0.1:4447
    09:53:24,173 INFO  [org.jboss.as.remoting] (MSC service thread 1-4) JBAS017100: Listening on localhost/127.0.0.1:9999
    09:53:24,361 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) JBAS015876: Starting deployment of "NewDB.war"
    09:53:25,159 INFO  [org.jboss.web] (MSC service thread 1-7) JBAS018210: Registering web context: /NewDB
    09:53:25,177 INFO  [org.jboss.as] (MSC service thread 1-2) JBAS015951: Admin console listening on http://127.0.0.1:9990
    09:53:25,179 INFO  [org.jboss.as] (MSC service thread 1-2) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 8275ms - Started 174 of 251 services (76 services are passive or on-demand)
    09:53:25,305 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "NewDB.war"
    09:53:26,815 ERROR [stderr] (http-localhost-127.0.0.1-8080-3) Error getting DS : javax.naming.NameNotFoundException: datasources/oracleDS -- service jboss.naming.context.java.jboss.datasources.oracleDS

    09:53:26,817 ERROR [stderr] (http-localhost-127.0.0.1-8080-3) javax.naming.NameNotFoundException: datasources/oracleDS -- service jboss.naming.context.java.jboss.datasources.oracleDS

    09:53:26,817 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)

    09:53:26,817 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)

    09:53:26,817 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214)

    09:53:26,817 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at p1.TestServlet.init(TestServlet.java:37)

    09:53:26,833 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at javax.servlet.GenericServlet.init(GenericServlet.java:242)

    09:53:26,833 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202)

    09:53:26,833 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:952)

    09:53:26,833 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188)

    09:53:26,833 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

    09:53:26,833 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

    09:53:26,849 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

    09:53:26,849 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

    09:53:26,849 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

    09:53:26,849 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

    09:53:26,849 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

    09:53:26,864 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

    09:53:26,864 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

    09:53:26,864 ERROR [stderr] (http-localhost-127.0.0.1-8080-3)   at java.lang.Thread.run(Unknown Source)

     javax.naming.NameNotFoundException

but i am getting success report in command prompt as fallows

16:53:33,981 INFO  [org.jboss.modules] JBoss Modules version 1.1.1.GA
16:53:34,215 INFO  [org.jboss.msc] JBoss MSC version 1.0.2.GA
16:53:34,262 INFO  [org.jboss.as] JBAS015899: JBoss AS 7.1.1.Final "Brontes" starting
16:53:35,452 INFO  [org.xnio] XNIO Version 3.0.3.GA
16:53:35,455 INFO  [org.jboss.as.server] JBAS015888: Creating http management service using socket-binding (management-http)
16:53:35,486 INFO  [org.xnio.nio] XNIO NIO Implementation Version 3.0.3.GA
16:53:35,540 INFO  [org.jboss.as.logging] JBAS011502: Removing bootstrap log handlers
16:53:35,541 INFO  [org.jboss.remoting] JBoss Remoting version 3.2.3.GA
16:53:35,582 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 31) JBAS010280: Activating Infinispan subsystem.
16:53:35,576 INFO  [org.jboss.as.configadmin] (ServerService Thread Pool -- 26) JBAS016200: Activating ConfigAdmin Subsystem
16:53:35,748 INFO  [org.jboss.as.osgi] (ServerService Thread Pool -- 39) JBAS011940: Activating OSGi Subsystem
16:53:35,739 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 38) JBAS011800: Activating Naming Subsystem
16:53:35,722 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 44) JBAS013101: Activating Security Subsystem
16:53:35,707 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
16:53:35,690 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
16:53:35,924 INFO  [org.jboss.as.security] (MSC service thread 1-4) JBAS013100: Current PicketBox version=4.0.7.Final
16:53:35,906 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-7) JBAS015400: Bound mail session [java:jboss/mail/Default]
16:53:35,885 INFO  [org.jboss.as.naming] (MSC service thread 1-1) JBAS011802: Starting Naming Service
16:53:35,817 INFO  [org.jboss.as.connector] (MSC service thread 1-8) JBAS010408: Starting JCA Subsystem (JBoss IronJacamar 1.0.9.Final)
16:53:36,479 INFO  [org.jboss.ws.common.management.AbstractServerConfig] (MSC service thread 1-4) JBoss Web Services - Stack CXF Server 4.0.2.GA
16:53:36,679 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-1) JBAS015012: Started FileSystemDeploymentService for directory C:\jboss\standalone\deployments
16:53:37,242 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
16:53:38,059 INFO  [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-5) Starting Coyote HTTP/1.1 on http--127.0.0.1-8080
16:53:38,059 INFO  [org.apache.coyote.ajp.AjpProtocol] (MSC service thread 1-4) Starting Coyote AJP/1.3 on ajp--127.0.0.1-8009
16:53:38,106 INFO  [org.jboss.as.remoting] (MSC service thread 1-6) JBAS017100: Listening on /127.0.0.1:4447
16:53:38,106 INFO  [org.jboss.as.remoting] (MSC service thread 1-2) JBAS017100: Listening on /127.0.0.1:9999
16:53:38,371 INFO  [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
JBAS014775:    New missing/unsatisfied dependencies:
      service jboss.jdbc-driver.ojdbc6_jar (missing) dependents: [service jboss.data-source.java:jboss/datasources/oracleDS]

16:53:38,434 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "NewDB.war"
16:53:38,434 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015876: Starting deployment of "ojdbc6.jar"
16:53:39,075 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) JBAS010403: Deploying JDBC-compliant driver class oracle.jdbc.OracleDriver (version 11.2)
16:53:39,075 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) JBAS010400: Bound data source [java:jboss/datasources/oracleDS]
16:53:39,184 INFO  [org.jboss.web] (MSC service thread 1-3) JBAS018210: Registering web context: /NewDB
16:53:39,200 INFO  [org.jboss.as] (MSC service thread 1-4) JBAS015951: Admin console listening on http://127.0.0.1:9990
16:53:39,215 INFO  [org.jboss.as] (MSC service thread 1-4) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 5516ms - Started 201 of 281 services (78 services are passive or on-demand)
16:53:39,262 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "ojdbc6.jar"
16:53:39,262 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "NewDB.war"
16:53:39,262 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014776:    Newly corrected services:
      service jboss.jdbc-driver.ojdbc6_jar (no longer required)

16:53:55,446 INFO  [stdout] (http--127.0.0.1-8080-2) **Success getting DS** : class org.jboss.jca.adapters.jdbc.WrapperDataSource
16:57:40,076 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015877: Stopped deployment NewDB.war in 81ms

I wrote fallowing code in Servlet... in eclipse luna.

package p1;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void init() throws ServletException {
        super.init();
        DataSource ds = null;
        Context ctx = null;
        try {
            String strDSName = "java:jboss/datasources/oracleDS";
            ctx = new InitialContext();
            Context cx = (Context)ctx.lookup("java:jboss");
            ds = (javax.sql.DataSource)cx.lookup("datasources/oracleDS");
            System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
            System.out.println("Success getting DS : " + ds.getClass());
        } catch (Exception e) {
            System.err.println("Error getting DS : " + e);
            e.printStackTrace();
        }

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Fallowing are the context.xml and web.xml placed in WebContent/META-INF and WebContent/WEB-INF/lib respectively

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/JNDI">
    <Resource name="jdbc/oracleDS" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="system" password="Evergren$12" driverClassName="oracle.jdbc.OracleDriver"
               url="jdbc:oracle:thin:@localhost:1521:ORCL" />
</Context>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<resource-ref>
 <description>oracle Datasource example</description>
 <res-ref-name>jdbc/oracleDS</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
  • Your context.xml file will be ignored as that is a Tomcat configuration file and you're running JBoss AS;
  • Your web.xml is badly formed and I'm surprised it does not result in a deployment error - at the very least it is likely ignored;
  • I believe that your code works from the command line because the deployment order changes subtly. When you run from Eclipse your webapp is being initialised before your datasource, probably because of explicit deployment instructions from Eclipse.

Normally, JBoss AS can work out the correct deployment order by analysing annotations or deployment descriptors. However you don't have either in a useable form.

As this is a JavaEE 6 compliant server, you should not need explicit JNDI lookups. Try the following instead:

package p1;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Resource(lookup="java:jboss/datasources/oracleDS")
    private DataSource ds;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    public void init() throws ServletException {
        super.init();
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println("Success getting DS : " + ds.getClass());

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM