web-dev-qa-db-ja.com

名前[jdbc / mydb]はこのコンテキストにバインドされていません

この質問はすでに何度か提起されており、それらすべてを確認しました。しかし、私はまだ私の問題を解決することができません。

誰かが私が間違っていることを特定するのを手伝ってもらえますか?

Database.jspにアクセスしようとすると、次のエラーメッセージが表示されます。

    HTTP Status 500 - An exception occurred processing JSP page /database.jsp at line 24

type Exception report

message An exception occurred processing JSP page /database.jsp at line 24

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

exception

org.Apache.jasper.JasperException: An exception occurred processing JSP page /database.jsp at line 24

21:         <%
22:                 Context init = new InitialContext();
23:                 Context env = (Context) init.lookup("Java:/comp/env/");
24:                 DataSource data = (DataSource) env.lookup("jdbc/mydb");
25:                 Connection conn = data.getConnection();
26:                 Statement stmt = conn.createStatement();
27: 


Stacktrace:
    org.Apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.Java:568)
    org.Apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.Java:455)
    org.Apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.Java:390)
    org.Apache.jasper.servlet.JspServlet.service(JspServlet.Java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
    org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)
root cause

javax.servlet.ServletException: javax.naming.NameNotFoundException: Name [jdbc/mydb] is not bound in this Context. Unable to find [jdbc].
    org.Apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.Java:916)
    org.Apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.Java:845)
    org.Apache.jsp.database_jsp._jspService(database_jsp.Java:185)
    org.Apache.jasper.runtime.HttpJspBase.service(HttpJspBase.Java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
    org.Apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.Java:432)
    org.Apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.Java:390)
    org.Apache.jasper.servlet.JspServlet.service(JspServlet.Java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.Java:727)
    org.Apache.Tomcat.websocket.server.WsFilter.doFilter(WsFilter.Java:52)

インストールに関する情報の一部を以下に示します。コンピューターはMac OS Xで実行されています。WebサーバーはTomcatバージョン7.0.52で、/ Users/username/Dropbox/Server/Apache-Tomcat-7.0.52の下にインストールされます。 Derby 10.10.1.1を組み込みデータベースとして実行しようとしています。フォルダー/Users/username/Dropbox/Server/Apache-Tomcat-7.0.52/libの下にderby.jarをコピーしました。 PATHファイルに次の内容を書きました(関連性があるかどうかわかりません):

export DERBY_HOME=/Users/username/Dropbox/Server/Apache-Tomcat-7.0.52/lib
export PATH=$PATH:$DERBY_HOME/bin

Web.xmlファイルの内容は次のとおりです。

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://Java.Sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://Java.Sun.com/xml/ns/javaee
                      http://Java.Sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
    <resource-ref>
       <description>DB Connection</description>
       <res-ref-name>jdbc/mydb</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
    </resource-ref>
  </description>

</web-app>

context.xml

<?xml version='1.0' encoding='utf-8'?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

server.xml

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.Apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.Apache.catalina.core.JasperListener" />
  <Listener className="org.Apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.Apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.Apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
        <!-- Added by LC #2 @19/3/2014 -->
            <!-- Global Datasource for Derby mydb database -->
            <Resource name="jdbc/mydb"
                     type="javax.sql.DataSource"  auth="Container"
                     description="Derby database for Stocktails"
                     maxActive="20" maxIdle="10" maxWait="-1"
                     username="" password="" 
                     driverClassName="org.Apache.derby.jdbc.EmbeddedDriver"
                     url="jdbc:derby:/Users/username/Dropbox/Server/Apache-Tomcat-7.0.52/databases/mydb"/>
        <!-- End #2 -->

  </GlobalNamingResources>

  <Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.Apache.catalina.realm.LockOutRealm">
        <Realm className="org.Apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.Apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

        <!-- Added by LC #1 @19/3/2014 -->
        <Context
           docBase="ROOT" path="" debug="0"
           reloadable="true">
        </Context>
        <!-- End #1 -->

      </Host>
    </Engine>
  </Service>
</Server>

最後に、database.jspページを示します。

<html>
<head>
  <title>Quote Query</title>
</head>
<body>
  <h1>retrieve stock quotes</h1>
  <h3>Choose Ticker(s):</h3>
  <form method="get">
    <input type="checkbox" name="ticker" value="AAPL">AAPL
    <input type="checkbox" name="ticker" value="MSFT">MSFT
    <input type="submit" value="Query">
  </form>

  <%
    String[] tickers = request.getParameterValues("ticker");
    if (tickers != null) {
  %>


        <%@ page import="javax.naming.*,Java.sql.*,javax.sql.*" %>
        <%
                Context init = new InitialContext();
                Context env = (Context) init.lookup("Java:/comp/env/");
                DataSource data = (DataSource) env.lookup("jdbc/mydb");
                Connection conn = data.getConnection();
                Statement stmt = conn.createStatement();

                String sqlStr = "SELECT * FROM Quotes WHERE Ticker IN (";
                sqlStr += "'" + tickers[0] + "'";  // First ticker
                for (int i = 1; i < tickers.length; ++i) {
                   sqlStr += ", '" + tickers[i] + "'";  // Subsequent tickers need a leading commas
                }
                sqlStr += ") ORDER BY Date ASC";

                // for debugging
                System.out.println("Query statement is " + sqlStr);

                ResultSet rset = stmt.executeQuery(sqlStr);
        %>

      <hr>
      <form method="get" action="quotes.jsp">
        <table border=1 cellpadding=5>
          <tr>
            <th>Ticker</th>
            <th>Date</th>
            <th>PriceOpen</th>
            <th>PriceHigh</th>
            <th>PriceLow</th>
            <th>PriceClose</th>
            <th>Volume</th>
            <th>PriceAdjClose</th>
          </tr>
  <%
      while (rset.next()) {
        int id = rset.getInt("id");
  %>
          <tr>
            <td><input type="checkbox" name="id" value="<%= id %>"></td>
            <td><%= rset.getString("Ticker") %></td>
            <td><%= rset.getString("Date") %></td>
            <td>$<%= rset.getInt("PriceOpen") %></td>
            <td><%= rset.getInt("PriceHigh") %></td>
            <td><%= rset.getString("PriceLow") %></td>
            <td><%= rset.getString("PriceClose") %></td>
            <td>$<%= rset.getInt("Volume") %></td>
            <td><%= rset.getInt("PriceAdjClose") %></td>
          </tr>
  <%
      }
  %>
        </table>
        <br>
        <input type="submit" value="Order">
        <input type="reset" value="Clear">
      </form>
      <a href="<%= request.getRequestURI() %>"><h3>Back</h3></a>
  <%
      rset.close();
      stmt.close();
      conn.close();
    }
  %>
</body>
</html>

ログ

Mar 21, 2014 7:57:26 AM org.Apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the Java.library.path: /Users/lcazarre/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/Java:.
Mar 21, 2014 7:57:26 AM org.Apache.Tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property.
Mar 21, 2014 7:57:28 AM org.Apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 21, 2014 7:57:28 AM org.Apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 21, 2014 7:57:28 AM org.Apache.catalina.startup.Catalina load
INFO: Initialization processed in 3227 ms
Mar 21, 2014 7:57:28 AM org.Apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 21, 2014 7:57:28 AM org.Apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.52
Mar 21, 2014 7:57:28 AM org.Apache.catalina.realm.UserDatabaseRealm startInternal
SEVERE: Exception looking up UserDatabase under key UserDatabase
javax.naming.NameNotFoundException: Name [UserDatabase] is not bound in this Context. Unable to find [UserDatabase].
    at org.Apache.naming.NamingContext.lookup(NamingContext.Java:819)
    at org.Apache.naming.NamingContext.lookup(NamingContext.Java:167)
    at org.Apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.Java:253)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.Java:201)
    at org.Apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.Java:120)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.ContainerBase.startInternal(ContainerBase.Java:1109)
    at org.Apache.catalina.core.StandardEngine.startInternal(StandardEngine.Java:300)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.StandardService.startInternal(StandardService.Java:443)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.StandardServer.startInternal(StandardServer.Java:731)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.startup.Catalina.start(Catalina.Java:689)
    at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:606)
    at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:321)
    at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:455)

Mar 21, 2014 7:57:28 AM org.Apache.catalina.realm.CombinedRealm startInternal
SEVERE: Failed to start "org.Apache.catalina.realm.UserDatabaseRealm/1.0" realm
org.Apache.catalina.LifecycleException: Failed to start component [Realm[UserDatabaseRealm]]
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:154)
    at org.Apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.Java:201)
    at org.Apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.Java:120)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.ContainerBase.startInternal(ContainerBase.Java:1109)
    at org.Apache.catalina.core.StandardEngine.startInternal(StandardEngine.Java:300)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.StandardService.startInternal(StandardService.Java:443)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.StandardServer.startInternal(StandardServer.Java:731)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.startup.Catalina.start(Catalina.Java:689)
    at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
    at Java.lang.reflect.Method.invoke(Method.Java:606)
    at org.Apache.catalina.startup.Bootstrap.start(Bootstrap.Java:321)
    at org.Apache.catalina.startup.Bootstrap.main(Bootstrap.Java:455)
Caused by: org.Apache.catalina.LifecycleException: No UserDatabase component found under key UserDatabase
    at org.Apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.Java:262)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    ... 17 more

Mar 21, 2014 7:57:31 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/docs
Mar 21, 2014 7:57:32 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/examples
Mar 21, 2014 7:57:33 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/Host-manager
Mar 21, 2014 7:57:33 AM org.Apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /Users/lcazarre/Dropbox/Work/MS50/COMSM0104/Server/Apache-Tomcat-7.0.52/webapps/manager
Mar 21, 2014 7:57:34 AM org.Apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 21, 2014 7:57:34 AM org.Apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 21, 2014 7:57:34 AM org.Apache.catalina.startup.Catalina start
INFO: Server startup in 6321 ms

ありがとう、LC

40
lcazarre

グローバルアプリケーションをWebアプリケーションで使用できるようにするには、META-INF/context.xmlファイルにResourceLinkが必要です。

 <ResourceLink name="jdbc/mydb"
             global="jdbc/mydb"
              type="javax.sql.DataSource" />
51
Mark Thomas

BitronixでTomcatを使用している場合、これにより問題が修正されます

エラーは、データソース'jdbc/mydb'のハンドラーが見つからなかったことを示しているため、Tomcatサーバーが必要に応じてbitronix構成ファイルを参照していることを確認する必要があります。

btm-config.propertiesおよびresources.propertiesファイルを使用してデータソースを設定している場合、Tomcatで次の2つのJVM引数を指定します。

(すでに使用している場合は、参照が正しいことを確認してください):

  • btm.root
  • bitronix.tm.configuration

例えば.

-Dbtm.root="C:\Program Files\Apache Software Foundation\Tomcat 7.0.59" 
-Dbitronix.tm.configuration="C:\Program Files\Apache Software Foundation\Tomcat 7.0.59\conf\btm-config.properties" 

次に、サーバーを再起動してログを確認します。

1
Naor Bar