【问题标题】:javax.naming.NameNotFoundException in Jetty but not in Tomcat. How to solve?Jetty 中的 javax.naming.NameNotFoundException 但在 Tomcat 中没有。怎么解决?
【发布时间】:2015-10-07 23:32:56
【问题描述】:

我有一个这样的示例代码:

ConnectionPool.dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/murach");

webapp/META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/murach"
          auth="Container"
          type="javax.sql.DataSource"
          username="root"
          --- Rest of the text ---/>
</Context>

当我将此 Web 应用程序部署到 Tomcat 时,数据库连接很好,但是当我尝试使用 Jetty 插件使用 Jetty 时:jetty:run-war

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.2.1.v20140609</version>
    <configuration>
        <scanIntervalSeconds>2</scanIntervalSeconds>
        <httpConnector>
            <port>8082</port>
        </httpConnector>
        <webApp>
            <contextPath>/</contextPath>
        </webApp>
    </configuration> 
 </plugin>

我得到:

 javax.naming.NameNotFoundException; remaining name 'jdbc/murach'

我怎样才能使这项工作也与码头一起工作?

我也试过添加

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

到 web.xml,但现在我得到:

java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default

【问题讨论】:

  • 这个数据源是为你的 Jetty 服务器定义的吗? https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
  • @Koray Tugay 请参阅下面的 m.hassaballah 的回答。您将在 web.xml 文件中将数据源定义为资源,而不仅仅是在 Context.xml 中。
  • 只是想知道...我的回答是否有帮助或者使用建议的方法是否存在一些问题?

标签: java maven tomcat jetty jndi


【解决方案1】:

我想将此作为评论添加,但我还不允许。
svaor 的答案是正确的,因为没有定义放置 JNDI 资源的标准位置在 J2EE 规范中。
所以,context.xml 文件是 tomcat 特有的,jetty 对此一无所知。

http://docs.jboss.org/jbossweb/3.0.x/jndi-resources-howto.html

J2EE 标准在 /WEB-INF/web.xml 文件引用资源;中引用的资源 这些元素必须在特定应用服务器中定义 配置。

【讨论】:

    【解决方案2】:

    Jetty 应配置为使用 JNDI 并将 JNDI 资源部署到其中。

    尝试添加到maven插件配置:

    <configuration>
       <!-- ... -->
       <jettyEnvXml>src/main/dev-path-to-jetty-env-xml/jetty-env.xml</jettyEnvXml>
       <webXml>src/main/dev-path-to-web-xml/web.xml</webXml>
    </configuration>
    

    文件jetty-env.xml 看起来像(仅作为示例;使用bonecp 数据源):

    <?xml version="1.0"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
        <New id="murachDataSource" class="org.eclipse.jetty.plus.jndi.Resource">
            <Arg>jdbc/murach</Arg>
    
            <Arg>
                <New class="com.jolbox.bonecp.BoneCPDataSource">
                    <Set name="driverClass">org.h2.Driver</Set>
                    <Set name="jdbcUrl">jdbc:h2:~/murach/test</Set>
                    <Set name="username">sa</Set>
                    <Set name="password"/>
    
                    <Set name="partitionCount">4</Set>
                    <Set name="minConnectionsPerPartition">5</Set>
                    <Set name="acquireIncrement">5</Set>
                    <Set name="closeConnectionWatch">false</Set>
                </New>
            </Arg>
        </New>
    </Configure>
    

    文件web.xml 应包含行:

    <resource-ref>
        <description>My DataSource Reference</description>
        <res-ref-name>jdbc/murach</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    

    【讨论】:

      【解决方案3】:

      您是否将资源引用添加到 web.xml?

       <resource-ref>
          <description>XX</description>
          <res-ref-name>jdbc/murach</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
        </resource-ref>
      

      【讨论】:

      • 感谢您的回答,但我得到:java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default
      猜你喜欢
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 2023-02-08
      • 2015-02-13
      • 2020-02-27
      • 2023-03-26
      相关资源
      最近更新 更多