2012-05-24 55 views
2

當在(tomcat 7)server.xml或Context.xml中配置數據源時,hibernate無法識別JNDI並在低於默認值時拋出,但在嘗試訪問時正常工作正常數據源在代碼中使用JNDI。我究竟做錯了Tomcat 7.0中的數據源JNDI配置Hibernate

下面是我在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 

在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> 

在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>** 

是我的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] 
+0

採取出server.xml中的全部。另外,當你說「context.xml」時,你的意思是你的webapp中的META-INF/context.xml,還是你的意思是Tomcat的全局conf/context.xml? –

回答

4

這是簡單得多,它可能看起來:

  1. 不要在你使用的是context.xml放一下JNDI東西在你的web.xml
  2. (全球或Web應用程序相對是否)使用適當的名稱,說 「JDBC /僱員」
  3. 冬眠配置的形式傳遞的名稱的 「java:comp/env的/ JDBC /僱員」

下面是參考頁:

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

相關問題