SEVERE: Exception looking up UserDatabase under key UserDatabase javax.naming.NameNotFoundException: Name [UserDatabase] is not bound in this Context

孤街浪徒 提交于 2021-02-11 09:52:06

问题


im using TOMCAT 9 and latest hibernat jars with java 1.8
i create simple user in mysql and 1 table db called foo1
in my webapp in tomcat i configured based on examples online :

workspace\HibernateApp\src\hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property>

        <property name="hibernate.max_fetch_depth">3</property>

        <property name="connection.datasource">java:comp/env/jdbc/foo1</property>

        <!-- Mapping files -->

        <!--mapping class="com.playground.myapp.model.User"/-->

    </session-factory>
</hibernate-configuration>

workspace\HibernateApp\WebContent\META-INF\context.xml

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true" path="/">
    <Resource
            name="jdbc/foo1"
            auth="Container"
            type="javax.sql.DataSource"
            username="foo1"
            password="12345"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/foo1"
            maxActive="8"
            maxIdle="4"/>

</Context>

Then im not sure way i need also to set up in server.xml the Resource but i did as i saw developers doing it online

workspace\Servers\Tomcat v9.0 Server at localhost-config\server.xml

<GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <!-- Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/-->
   <Resource
            name="jdbc/foo1"
            auth="Container"
            type="javax.sql.DataSource"
            username="foo1"
            password="12345"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/foo1"
            maxActive="8"
            maxIdle="4"/>
  </GlobalNamingResources>

but i keep getting this runtime error:

SEVERE: Exception looking up UserDatabase under key UserDatabase
javax.naming.NameNotFoundException: Name [UserDatabase] is not bound in this Context. Unable to find [UserDatabase].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.catalina.realm.UserDatabaseRealm.startInternal(UserDatabaseRealm.java:215)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.realm.CombinedRealm.startInternal(CombinedRealm.java:245)
    at org.apache.catalina.realm.LockOutRealm.startInternal(LockOutRealm.java:115)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:926)
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:793)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:655)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)

来源:https://stackoverflow.com/questions/41630337/severe-exception-looking-up-userdatabase-under-key-userdatabase-javax-naming-na

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!