有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用于Hibernate的Tomcat7.0中的java数据源JNDI配置

在(tomcat 7)服务器中配置数据源时。xml或在上下文中。在xml中,hibernate无法识别JNDI和抛出下面的expetion,但当我在代码中尝试使用JNDI访问数据源时,同样可以正常工作。我做错了什么

下面是我在hibernate中的配置。属性

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

在服务器中。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>

在上下文中。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>**

最后,下面是我的网站。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]

共 (1) 个答案

  1. # 1 楼答案

    这比看起来要简单:

    1. 不要把关于JNDI的任何东西放在你的网站上。xml
    2. 在上下文中。您正在使用的xml(无论是全局的还是相对于webapp的)使用正确的名称,比如“jdbc/employee”
    3. 要休眠配置,请以“java:comp/env/jdbc/employee”的形式传递名称

    以下是参考页:

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