JNDI在服务器初始化函数

时间:2015-05-26 18:33:16

标签: java tomcat servlets jndi

Howdie do,

我正在尝试将应用程序部署到我的新Tomcat托管服务。问题是我现在正在运行服务器初始化函数时抛出一个Servlet异常。

错误如下:

26-May-2015 12:56:48.668 SEVERE [http-nio-12937-exec-2] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet Controller
 javax.servlet.ServletException
    at controller.Controller.init(Controller.java:42)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1241)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)

现在,这是我设置数据源的web.xml文件:

<Resource name="jdbc/drkplaya_pkRater" auth="Container"
             type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
             url="jdbc:mysql://localhost:3306/drkplaya_pkRater"
             username="drkplaya_pkRater" password="" initialSize="1" maxActive="30" maxIdle="30"
             maxWait="15000" removeAbandoned="true" removeAbandonedTimeout="300" logAbandoned="true"
             validationQuery="select now();" 
    />
  <servlet>
    <description></description>
    <display-name>Controller</display-name>
    <servlet-name>Controller</servlet-name>
    <servlet-class>controller.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>/gallery</url-pattern>
  </servlet-mapping>

最后,这是我调用数据源的init函数:

public void init(ServletConfig config) throws ServletException {
        try {
            InitialContext initContext = new InitialContext();
            Context env = (Context) initContext.lookup("java:comp/env");

            ds = (DataSource) env.lookup("jdbc/drkplaya_pkRater");

        } catch (NamingException e) {
            throw new ServletException();
        }
    }

现在,这在我的本地服务器上运行100%,但在上传后,我收到上面列出的例外。

我的init函数中是否缺少导致此错误的内容?

编辑:有人指出我正在吞下NameException。解决之后,我知道有以下错误:

message javax.naming.NameNotFoundException: Name [jdbc/drkplaya_pkRater] is not bound in this Context. Unable to find [jdbc].

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.naming.NameNotFoundException: Name [jdbc/drkplaya_pkRater] is not bound in this Context. Unable to find [jdbc].
    controller.Controller.init(Controller.java:42)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:744)
root cause

javax.naming.NameNotFoundException: Name [jdbc/drkplaya_pkRater] is not bound in this Context. Unable to find [jdbc].
    org.apache.naming.NamingContext.lookup(NamingContext.java:818)
    org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    controller.Controller.init(Controller.java:39)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

3 个答案:

答案 0 :(得分:1)

您的mysql服务器可能在不同于tomcat的主机上运行。您将本地主机放在资源配置中。请咨询您的托管服务提供商您的数据库服务器的主机。

答案 1 :(得分:1)

由于这个问题,它在本地计算机上运行良好:

url="jdbc:mysql://localhost:3306/drkplaya_pkRater"
         username="drkplaya_pkRater" password=""

但是当您在主机服务器上部署相同的web.xml时,localhost会引用该特定的主机服务器。

所以你有两个选择:

1)确保在具有该特定用户名的mysql上的3306端口上运行host server服务器。

(或)

2)更改url=username, password以匹配特定mysql上的host server数据库实例。

答案 2 :(得分:0)

这个问题有几件事

1)我的apache / lib目录中没有mysql-connector

2)我从未设置我的context.xml文件以包含服务器应连接的上下文。所以我将以下内容添加到我的context.xml文件中:

  <Resource name="jdbc/drkplaya_pkRater" auth="Container"
             type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
             url="jdbc:mysql://mysql3000.mochahost.com:3306/drkplaya_pkRater"
             username="drkplaya_pkRater" password="" initialSize="1" maxActive="30" maxIdle="30"
             maxWait="15000" removeAbandoned="true" removeAbandonedTimeout="300" logAbandoned="true"
             validationQuery="select now();" 
    />

3)我必须更新我的web.xml文件,以便只引用在context.xml文件中创建的资源:

<resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/drkplaya_pkRater</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

4)感谢BalusC指出我在init函数中吞下了异常。所以它已改为:

public void init(ServletConfig config) throws ServletException {
        try {
            InitialContext initContext = new InitialContext();
            Context env = (Context) initContext.lookup("java:comp/env");

            ds = (DataSource) env.lookup("jdbc/drkplaya_pkRater");

        } catch (NamingException e) {
            throw new ServletException(e);
        }
    }

我现在已成功连接到数据源。

再次感谢