Netbeans Dev to Tomcat Production: DB connection not found

I have developed a Java web application in Netbeans 6.5 using MySQL and Hibernate database. Development server and development application server (Tomcat 6) are on my development machine. Everything is working; the application is fetching data from the database correctly.

Now I'm ready to move it to the production server. Again, the DB server and the application server are on the same machine. I am deploying a WAR file and trying to access the application; I can access static pages but servlets that use the database error except:

org.hibernate.exception.JDBCConnectionException: Cannot open connection

I'm pretty sure the problem is because Tomcat doesn't know about the data source. Netbeans seems to handle this for me. I read that I might need to add a RESOURCE entry, so I took some advice from this site that gave me the context.xml:

<?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:

<?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">
    <!-- 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 hibernate.cfg.xml:

<?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">
<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 am getting the error org.hibernate.HibernateException: Could not find datasource

.

Am I on the right track to move from development to production? What am I missing?

+1


a source to share


3 answers


I think you are on the right track. I first set up a datasource and would check it out of the hibernation side. Here's a good article: 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 file, hibernate.cfg.xml

I think you should try changing hibernate.connection.datasource

tojdbc/employeedirectory

+1


a source


jndi datasource must be defined in /tomcat/server.xml file, see Tomcat JNDI Datasource user manual , not webapp / context. XML



+1


a source


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

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

0


a source







All Articles