views:

1009

answers:

3

I developed a Java web application in Netbeans 6.5 using a MySQL database and Hibernate. Both the development database server and development application server (Tomcat 6) reside on my development machine. Everything works; the application pulls data from the database correctly.

Now, I'm ready to move it to the production server. Again, the DB server and app server are on the same machine. I deploy the WAR file and try to access the application; I can access the static pages but the Servlets that use the database error out with the exception:

org.hibernate.exception.JDBCConnectionException: Cannot open connection

I'm pretty sure the problem relates to Tomcat not knowing about the data source. It seems as if Netbeans handles this for me. I've read that I might need to add a RESOURCE entry so I took some advice from this site which gave me a context.xml of:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/EmployeeDirectory">
     <Resource   
          name="jdbc/employeedirectory"  auth="Container"
          type="javax.sql.DataSource"    username="EmployeeDir"
          password="EmployeeDirectory"   driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://127.0.0.1:3306/EmployeeDirectory?autoReconnect=true"
          maxActive="15"                 maxIdle="7"
          validationQuery="Select 1" />
</Context>

a web.xml of:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
    <!-- Omit Servlet Info -->
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/employeedirectory</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

and a hibernate.cfg.xml of:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.datasource">java:comp/env/jdbc/employeedirectory</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- Omit other Mappings -->
        <mapping class="EmployeeDirectory.data.PhoneNumber" resource="EmployeeDirectory/data/PhoneNumber.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Now, I get a org.hibernate.HibernateException: Could not find datasource error.

Am I on the right path for moving from development to production? What am I missing?

A: 

I think you are on the right track. I would first set up the datasource and verify it out side of hibernate. Here is a good article on that: http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.htm and some examples here: http://www.mbaworld.com/docs/jndi-datasource-examples-howto.html

Then, I would configure hibernate to use the datsource. From looking at your hibernate.cfg.xml file I think you should try changing hibernate.connection.datasource to jdbc/employeedirectory

Ryan Cook
A: 

the jndi datasource should be defined in /tomcat/server.xml see Tomcat JNDI Datasource how-to and not in webapp/context.xml

Michael Lange
A: 

Tomcat 6 requires that you add the resource tag to the context.xml, not the server.xml. You could in Tomcat 5.x. I have it working fine in a separate install of Tomcat, but I'm still trying to use connection pooling inside NB 6.5.

That same Apache site has a link to the Tomcat 6 version of JNDI and it tells you to add the resource tag to the context.xml.