Since it takes me 8 hours to get a hibernate project running on a Tomcat server due to problems with setting up the database i want to post here a working configuration.
Problem:
Hibernate3, datasource confiured as container managed JDBC, MYSQL database, always failed to connect to the database with the following error
Code:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
Solution:Only when adding a "java:comp/env/" in front of the jdbc name in the hibernate configuration, hibernate is able to connect.
e.g.:
Code:
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/engineDB</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
[...]
</hibernate-configuration>
In the tomcat configuration file server.xml the according entry for the JDBC datatabase resource must be defined in the Server->Service->Engine->Host tag inside a context for the according web application:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Server>
[...]
<GlobalNamingResources>
[...]
</GlobalNamingResources>
<Service name="Catalina">
[...]
<Engine
defaultHost="localhost"
name="Catalina">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
<Host appBase="webapps" name="localhost">
<Context path="/engine_web" docBase="engine_web">
<Resource name="jdbc/engineDB" auth="Container" scope="Shareable" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="*****" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.0.8/engineDB?autoReconnect=true"/>
</Context>
</Host>
</Engine>
</Service>
</Server>
Finally the common/lib directory must contain the database driver.
Hopefully this is helpful for some of you ...