无法找到JNDI资源Tomcat 7

时间:2015-03-05 02:41:21

标签: java tomcat7

在我的测试机器上的eclipse中一切正常,但是当我将WAR部署到服务器时,我收到了#34;找不到JNDI资源"每次尝试连接数据库时出错。

我在WEB-INF / context.xml中使用资源。 mysql-connector-java-5.1.25-bin.jar位于应用程序的lib文件夹中。我已经尝试了所有我能想到的东西。我假设它是一个特定于tomcat的问题,因为它在eclipse中的tomcat安装上工作正常。我缺少一个设置吗?

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>

产生的错误:

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)

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>

2 个答案:

答案 0 :(得分:1)

尝试使用:

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

我从这里得到了这些信息:http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Definitions

答案 1 :(得分:1)

原来我没有在CATALINA_HOME / lib文件夹中包含mysql连接器jar。我的印象是在应用程序的lib文件夹中使用连接器就足够了,但我错了。

来自另一个stackoverflow问题/答案:Tomcat does not recognize the MySQL .jar library

  

它不能在WEB-INF / lib中工作,因为容器需要访问库,并且它将它放在一个Web应用程序的类加载器中,而不是容器中。虽然我会想象CLASSPATH方法会起作用,但这不是标准的方法。也许还有一些阻碍它工作的障碍。