2

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)
user63898
  • 29,839
  • 85
  • 272
  • 514
  • 4
    You removed the `UserDatabase` resource from the `` of `server.xml`. Put it back in. See [UserDatabase Resources](http://tomcat.apache.org/tomcat-8.5-doc/jndi-resources-howto.html#UserDatabase_Resources) – Andreas Jan 13 '17 at 08:41
  • 1
    Thanks it help ! why do i need also the Resource in server.xml you have idea? – user63898 Jan 13 '17 at 09:10

0 Answers0