java Tomcat 7.0 for Hibernate 中的数据源 JNDI 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10731417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:17:31  来源:igfitidea点击:

Datasource JNDI configuration in Tomcat 7.0 for Hibernate

javahibernatetomcatdatasource

提问by Dragon

When Datasource is configured in (tomcat 7) server.xml or in Context.xml, hibernate is not able to identify JNDI and throwing below excpetion but same is working fine when I tried to access datasource using JNDI in the code. What am doing wrong

当在 (tomcat 7) server.xml 或 Context.xml 中配置数据源时,hibernate 无法识别 JNDI 并抛出以下异常,但当我尝试在代码中使用 JNDI 访问数据源时,同样工作正常。做错了什么

Below is my configuration in hibernate.properties

下面是我在 hibernate.properties 中的配置

hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.pool_size 10
hibernate.dialect org.hibernate.dialect.MySQL5Dialect
hibernate.connection.datasource java:comp/env/jdbc/employee
hibernate.show_sql true
hibernate.format_sql true

in server.xml

在 server.xml 中

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

    <!-- SingleSignOn valve, share authentication between web applications
         Documentation at: /docs/config/valve.html -->
    <!--
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    -->

    <!-- Access log processes all example.
         Documentation at: /docs/config/valve.html
         Note: The pattern used is equivalent to using pattern="common" -->
    <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" resolveHosts="false"/>
    **<Context docBase="hibernate" path="/hibernate">
            <Resource name="jdbc/employee" auth="Container"
          type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:45000/hibernatetest"
          username="user" password="hibernate" maxActive="20" maxIdle="10"
          maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>
        </Context>**
  </Host>

In Context.xml file

在 Context.xml 文件中

    **<Context docBase="hibernate" path="/hibernate">
        <Resource name="jdbc/employee" auth="Container"
          type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:45000/hibernatetest"
          username="user" password="hibernate" maxActive="20" maxIdle="10"
          maxWait="-1" removeAbandoned="true" removeAbandonedTimeout="60" logAbandoned="true"/>
    </Context>**

Finally below is my web.xml file

最后下面是我的 web.xml 文件

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
    **<web-app >
        <servlet>
            <servlet-name>employee</servlet-name>
            <servlet-class>edu.experiments.web.EmployeViewer</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>employee</servlet-name>
            <url-pattern>/employee</url-pattern>        
        </servlet-mapping>

        <resource-ref>
            <description>DB Connection</description>
            <res-ref-name>jdbc/employee</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
        </resource-ref>
    </web-app>**


org.hibernate.service.jndi.JndiException: Unable to lookup JNDI name [java:comp/env/jdbc/employee]

回答by Pietro Polsinelli

It is simpler than it may seem:

它比看起来更简单:

  1. don't put anything about JNDI in your web.xml
  2. in the context.xml you are using (whether global or webapp relative) use the proper name, say "jdbc/employee"
  3. to hibernate configuration pass the name in the form "java:comp/env/jdbc/employee"
  1. 不要在 web.xml 中放入任何关于 JNDI 的内容
  2. 在您使用的 context.xml 中(无论是全局的还是 webapp 相关的)使用正确的名称,比如“jdbc/employee”
  3. 休眠配置以“java:comp/env/jdbc/employee”形式传递名称

Here is the reference page:

这是参考页面:

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html