简体   繁体   中英

JNDI throwing Servlet exception in server init function

Howdie do,

I'm attempting to deploy an application to my new Tomcat hosting service. The issue is that I'm now getting a Servlet exception thrown when the server init function runs.

The error is below:

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)

Now, this is my web.xml file that sets up the datasource:

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

And finally, this is my init function that calls the datasource:

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();
        }
    }

Now, this runs 100% on my local server, but upon uploading it, I receive the exception listed above.

Is there something I'm missing in my init function that is causing this error?

EDIT: It was pointed out that I was swallowing the NameException. After fixing that, I know have the following error:

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)

Your mysql server runs probably on different host than tomcat. You placed local host in your Resource config. Please consult your hosting provider what is the host of your database server.

It runs fine on your local machine because of this:

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

but when you deploy same web.xml on host server, the localhost refers that particular host server.

So you have two options:

1) Either make sure there is a mysql server running on 3306 port on that host server with that particular user name.

(or)

2) Change the url= and username, password to match with mysql database instance on that particular host server .

The issue was a couple of things

1) I didn't have the mysql-connector in my apache/lib directory

2) I never setup my context.xml file to include the context for which the server should be connecting. So I added the following to my context.xml file:

  <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) I had to update my web.xml file to just reference the resource created in the context.xml file:

<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) Thank you to BalusC for pointing out that I was swallowing the exception in my init function. So it has been changed to:

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);
        }
    }

I am now successfully connecting to the datasource.

Thanks again

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