【问题标题】:javax.naming.NamingException, Tomcat7 and Struts2 can't access to DB resource via JNDIjavax.naming.NamingException,Tomcat7和Struts2无法通过JNDI访问数据库资源
【发布时间】:2012-03-28 10:12:38
【问题描述】:

我正在使用 Struts2 框架、eclipse 开发新的 Web 应用程序,并且我已经使用 Hibernate 映射了 db 结构。问题是,当我执行“列表”操作时,框架会返回此错误:

Struts 问题报告 Struts 检测到一个未处理的异常: 留言: 必须通过 java: URL 访问此上下文 无法查找 JNDI 名称 [java:comp/env/jdbc/main] 文件:org/apache/naming/SelectorContext.java 行号:776

server.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
    <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>
        <Resource auth="Container" defaultAutoCommit="false" driverClassName="net.sourceforge.jtds.jdbc.Driver" maxActive="200" maxIdle="100" maxWait="20000" name="jdbc/main" password="########" type="javax.sql.DataSource" url="jdbc:jtds:sqlserver://10.4.1.85/SBAGRU_DEV" username="****"/>

    </GlobalNamingResources>
    <Service name="Catalina">
        <Connector connectionTimeout="2000" executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

        <Connector SSLEnabled="true" clientAuth="false" keystoreFile="C:\jpeople.bin" keystorePass="gesbanke" keystoreType="PKCS12" maxThreads="200" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="SSL"/>

        <!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/> -->
        <Engine defaultHost="localhost" name="Catalina">
            <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false"> 
                <Context docBase="mag0" path="/mag0" reloadable="true" source="org.eclipse.jst.jee.server:mag0">
                    <ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" />
                </Context>
            </Host>
        </Engine>
    </Service>
</Server>

我也有 hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
    <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>
        <Resource auth="Container" defaultAutoCommit="false" driverClassName="net.sourceforge.jtds.jdbc.Driver" maxActive="200" maxIdle="100" maxWait="20000" name="jdbc/main" password="########" type="javax.sql.DataSource" url="jdbc:jtds:sqlserver://10.4.1.85/SBAGRU_DEV" username="****"/>

    </GlobalNamingResources>
    <Service name="Catalina">
        <Connector connectionTimeout="2000" executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

        <Connector SSLEnabled="true" clientAuth="false" keystoreFile="C:\jpeople.bin" keystorePass="******" keystoreType="PKCS12" maxThreads="200" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="SSL"/>

        <!--<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/> -->
        <Engine defaultHost="localhost" name="Catalina">
            <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false"> 
                <Context docBase="mag0" path="/mag0" reloadable="true" source="org.eclipse.jst.jee.server:mag0">
                    <ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" />
                </Context>
            </Host>
        </Engine>
    </Service>
</Server>

有什么建议可以看吗?我真的要疯了!

【问题讨论】:

  • 您还没有发布休眠配置。它与 server.xml 的配置相同。

标签: java database hibernate tomcat struts2


【解决方案1】:

我找到了解决方案,以及真正的问题。我在Eclipse中编辑了server.xml,并没有看到真正的发布结果。出于奇怪的原因 eclipse 删除了真正的 server.xml 中的资源链接,但在 eclipse 视图中显示链接。所以我谷歌了一整天,我发现我必须在 WEB-INF 目录下的 context.xml 文件中添加:

<?xml version="1.0" encoding="UTF-8"?> 
<Context antiJARLocking="true" path="">
<ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" /> 
</Context>

【讨论】:

    【解决方案2】:

    你的 web.xml 中有资源的链接吗:

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

    有关更多信息,请查看 Tomcat 文档:http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

    【讨论】:

    • 感谢您的快速回答,但这并不能解决问题。
    • 我找到了解决方案……以及真正的问题:
    【解决方案3】:

    一般JDBC数据源也配置为&lt;Resource&gt;如下

      <Resource name="jdbc/main" auth="Container" type="javax.sql.DataSource"
                   maxActive="100" maxIdle="30" maxWait="10000"
                   username="******" password="######" driverClassName="net.sourceforge.jtds.jdbc.Driver"
                   url="jdbc:jtds:sqlserver://10.4.1.85/SBAGRU_DEV"/>
    

    你可以试试这个内部标签。只需将&lt;ResourceLink&gt; 替换为上面的&lt;Resource&gt; 元素即可。

    您所做的似乎是正确的,因为&lt;ResourceLink&gt; 用于链接&lt;GlobalNamingResources&gt; 中定义的资源,不确定为什么它不起作用。

    【讨论】:

    • 感谢您的回答!我找到了解决方案真正的问题。我在 Eclipse 中编辑了 server.xml,并没有看到真正的发布结果。出于奇怪的原因,eclipse 删除了真实 server.xml 中的资源链接,将它显示在 eclipse 上。所以我谷歌了一整天,我发现我必须在 WEB-INF 目录下的 context.xml 文件中添加:&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;Context antiJARLocking="true" path=""&gt; &lt;ResourceLink global="jdbc/main" name="jdbc/main" type="javax.sql.DataSource" /&gt; &lt;/Context&gt;
    【解决方案4】:

    请看一下这个链接 Hibernate + Tomcat 7

    我也遇到过同样的问题:Tomcat更新后,这个bug就消失了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      相关资源
      最近更新 更多