我正在使用TOMCAT 9和最新的hibernat jar与java 1.8
我在mysql和1个表db中创建了一个名为foo1的简单用户
在我的tomcat webapp中,我根据在线示例配置:
工作空间\ 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>
工作空间\ 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>
然后我不确定我还需要在server.xml中设置资源,但我做了 正如我看到开发人员在网上做的那样
工作区\ Servers \ Tomcat v9.0服务器位于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>
但我不断收到此运行时错误:
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)