ryandlf
ryandlf

Reputation: 28545

Cannot Locate JNDI Resource Tomcat 7

Everything works fine in eclipse on my test machine, but when I deploy the WAR to the server I receive "Cannot locate JNDI Resource" errors on each attempt to connect with the database.

I am using a resource within WEB-INF/context.xml. The mysql-connector-java-5.1.25-bin.jar is located within the lib folder of the app. I've tried everything I can think of. I'm assuming its a tomcat specific problem because it works fine on the tomcat installation in eclipse. Is there a setting I am missing?

The context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
        name="jdbc/appointmentree" type="javax.sql.DataSource"
        maxActive="100" maxIdle="30" maxWait="10000"
        url="jdbc:mysql://1.1.1.1/appointmentree"
        driverClassName="com.mysql.jdbc.Driver"
        username="appointmentree" password="password"
    />
</Context>

The resulting error:

fm.sprout.java.db.DatabaseConnectionException: appointmentree is missing in JNDI.
    fm.sprout.java.db.DBConnection.<init>(DBConnection.java:17)
    fm.sprout.java.db.MySqlDBHandler.find(MySqlDBHandler.java:108)
    com.appointmentree.db.DBUser.findAll(DBUser.java:173)
    com.appointmentree.db.DBUser.find(DBUser.java:146)
    com.appointmentree.db.DBUser.find(DBUser.java:142)
    com.appointmentree.obj.User.getInstance(User.java:39)
    com.appointmentree.seeds.Login$1.handle(Login.java:39)
    fm.sprout.java.forms.FormHandler.init(FormHandler.java:53)
    fm.sprout.java.views.SeedBuilder.post(SeedBuilder.java:103)
    com.appointmentree.seeds.Login.onLoad(Login.java:33)
    fm.sprout.java.views.SeedPlanter.sow(SeedPlanter.java:131)
    fm.sprout.java.session.RequestHandler.doPost(RequestHandler.java:30)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    com.appointmentree.session.UserSessionFilter.doFilter(UserSessionFilter.java:72)
root cause

javax.naming.NamingException: com.mysql.jdbc.Driver
    org.apache.naming.NamingContext.lookup(NamingContext.java:859)
    org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    org.apache.naming.NamingContext.lookup(NamingContext.java:830)
    org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    org.apache.naming.NamingContext.lookup(NamingContext.java:830)
    org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    org.apache.naming.NamingContext.lookup(NamingContext.java:830)
    org.apache.naming.NamingContext.lookup(NamingContext.java:167)
    org.apache.naming.SelectorContext.lookup(SelectorContext.java:156)
    javax.naming.InitialContext.lookup(InitialContext.java:411)
    fm.sprout.java.db.DBConnection.<init>(DBConnection.java:15)
    fm.sprout.java.db.MySqlDBHandler.find(MySqlDBHandler.java:108)
    com.appointmentree.db.DBUser.findAll(DBUser.java:173)
    com.appointmentree.db.DBUser.find(DBUser.java:146)
    com.appointmentree.db.DBUser.find(DBUser.java:142)
    com.appointmentree.obj.User.getInstance(User.java:39)
    com.appointmentree.seeds.Login$1.handle(Login.java:39)
    fm.sprout.java.forms.FormHandler.init(FormHandler.java:53)
    fm.sprout.java.views.SeedBuilder.post(SeedBuilder.java:103)
    com.appointmentree.seeds.Login.onLoad(Login.java:33)
    fm.sprout.java.views.SeedPlanter.sow(SeedPlanter.java:131)
    fm.sprout.java.session.RequestHandler.doPost(RequestHandler.java:30)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    com.appointmentree.session.UserSessionFilter.doFilter(UserSessionFilter.java:72)

The resource in web.xml

<resource-env-ref>
    <resource-env-ref-name>jdbc/appointmentree</resource-env-ref-name>
    <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
  </resource-env-ref>

Upvotes: 1

Views: 1052

Answers (2)

ryandlf
ryandlf

Reputation: 28545

Turns out I hadn't included the mysql connector jar in the CATALINA_HOME/lib folder. I was under the impression that having the connector in the lib folder of the application was sufficient but I was wrong.

From another stackoverflow question/answer: Tomcat does not recognize the MySQL .jar library

It's not going to work in WEB-INF/lib since the container needs access to the library, and, that is putting it in the classloader of one web app, not the container. While I would have imagined the CLASSPATH approach would work, it's not the standard way to do this. Perhaps there is some other snag that's preventing it from working.

Upvotes: 1

Elvermg
Elvermg

Reputation: 447

Try using this:

<resource-ref>
    <res-ref-name>jdbc/appointmentree</res-ref-name>
    <res-ref-type>javax.sql.DataSource</res-ref-type>
    <res-auth>Container</res-auth>
  </resource-ref>

I got this information from here: http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Definitions

Upvotes: 1

Related Questions