当前位置: 首页 > 知识库问答 >
问题:

使用JNDI连接到JBoss7中创建的数据源时,eclipse中的javax.naming.NameNotFoundException

田冥夜
2023-03-14

我对J2EE是新手。我试图连接到使用JNDI在jboss AS 7中创建的数据源。

我使用Jboss作为7独立服务器来部署我的项目newdb.war。我已经在Jboss AS 7中创建了休闲数据源java:Jboss/datasources/oracleds(这是我的JNDI名称),并成功连接到Oracle数据库。

我正在使用oracle数据库存储我的sql表。

我在Eclipse中得到了休耕错误消息。但我在Propmpt命令中得到了成功的消息。我在eclipse中创建了Dynamic web project来执行所有这些任务。我不确定为我的任务创建哪种类型的项目。如果我错了请纠正我。

如果有人帮助走出这个错误,这将是很大的帮助。

        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

但我在命令提示符中以法洛斯的身份获得成功报告

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

我在servlet中编写了休闲代码...月食中。

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
    }

}

flowing是分别放在WebContent/META-INF和WebContent/WEB-INF/lib中的context.xml和web.xml

<?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>

共有1个答案

潘衡
2023-03-14
  • 您的context.xml文件将被忽略,因为它是Tomcat配置文件,并且您正在运行JBoss as;
  • 您的web.xml格式错误,我很惊讶它没有导致部署错误--至少它可能被忽略;
  • 我相信您的代码可以从命令行运行,因为部署顺序会发生微妙的变化。当您从Eclipse运行时,您的webapp是在您的数据源之前被初始化的,这可能是因为Eclipse的显式部署指令。

通常,JBoss AS可以通过分析注释或部署描述符来确定正确的部署顺序。但是,您没有任何可用的形式。

由于这是一个兼容JavaEE6的服务器,您应该不需要显式的JNDI查找。请尝试以下操作:

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
    }

}
 类似资料:
  • 问题内容: 我正在使用Websphere Portal 7.0并使用RAD 8.0创建一个portlet。我的portlet试图建立到远程服务器的db2连接。我在本地编写了一个Java程序来与服务器建立基本的JDBC连接,并从表中获取记录。代码工作正常;但是,当我将代码以及db2jcc4.jar添加到我的portlet时,连接不起作用。我正在使用基本的: 我认为使用Websphere数据源是正确的

  • 问题内容: 大家好,我使用Hibernate + Struts2 + Tomcat6 + Mysql作为我的J2EE框架。我一直在使用hibernate的内置连接池机制,但事实证明,在8小时后mysql关闭其连接的地方,这是一个问题。无论如何,我在Google上搜索了一下,发现我应该通过JNDI数据源获得连接,但是我无法获得关于此的完整的教程。我应该采取什么步骤呢?请提供足够的详细信息,对此我有点

  • 我无法连接到两个数据源使用在JDNI与Spring Boot。 生成以下stacktrace: 我做错了什么?

  • 我是JNDI的新手,我正在尝试让我的db连接正常工作。到目前为止还没有什么进展。我要么收到一条消息,说明“名称[java:comp/env]未在此上下文中绑定。找不到[java:comp]”,要么收到超时。 下面是关于我当前配置的信息。 Tomcat\conf\context.xml 我还尝试将资源放在context.xml中,以确保可以找到它: Tomcat\conf\server.xml 我回

  • 我有一个spring启动的quartz scheduler应用程序,以前在默认的quartz数据源下运行良好。但是,在移动到使用JBOSS JNDI数据源之后,它在Oracle12数据库中的BLOB列上遇到了异常。 我有一个定制的REST API,它将调用quartz API来调度作业。应用程序在错误页面和log4j调试日志文件中都没有返回关于异常的太多信息。 下面是关于异常的错误页面: 在我的l

  • 请知道,我对数据库很陌生。我能够正确安装mySQL和java连接器驱动程序。但每当我在eclipse中运行程序并尝试从我创建的数据库中检索信息时,我都会收到以下消息:“需要SSL连接,但服务器不支持”。下面是我要使用安全SSL连接运行的代码: `公共静态void main(字符串[]参数){