嵌入的Tomcat:尝试获取数据源时出现NoInitialContextException [英] Tomcat embedded: NoInitialContextException when trying to get a DataSource

查看:122
本文介绍了嵌入的Tomcat:尝试获取数据源时出现NoInitialContextException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要使用嵌入的tomcat运行的JSF Web应用程序.到目前为止,它已经可以工作了(包括在以下代码段的context.xml中指定的JDBCRealm),除了登录后我的代码无法实际获取源中指定的连接资源,并抛出NoInitialContextException.

I have a JSF web application that I'd like to run using tomcat embedded. It's working so far [including JDBCRealm, specified in the context.xml in the following code snippet], except after login my code is unable to actually get the connection resource specified in the source, throwing a NoInitialContextException.

我可能缺少明显的东西,但是关于嵌入的tomcat的访问量似乎很少. [Tomcat7.0.47,JDK7].

I'm probably missing something obvious, but there seems to be very little traffic out there regarding tomcat embedded. [Tomcat7.0.47, JDK7].

按照 per 其他此网站上 a>在此站点上,我尝试了多种变体来添加初始环境,但是我无法弄清楚这是否真的是我的问题,或者我只是没有找到适合此tomcat服务器的咒语

As per other questions on this site on this site, I've tried a number of variants on adding an initial environment, but I haven't been able to figure out if that's really my problem, or I just haven't found the right incantation for this tomcat server, yet.

Tomcat启动代码:

Tomcat launching code:

tomcat = new Tomcat();
tomcat.setPort(port);
tomcat.setBaseDir(".");
Context ctx = tomcat.addWebapp("/" + appname, appname);
// The login realm specified in this XML file is a JDBC realm, and the server correctly logs users in, so I believe this is parsed.
ctx.setConfigFile(new URL("file:///home/chunky/src/aqmt/AQMTEmbed/webapps/AQMTWeb/META-INF/context.xml"));

ContextResource resource = new ContextResource();
resource.setName("jdbc/aqmtwebdb");
resource.setAuth("Container");
resource.setType("javax.sql.DataSource");
resource.setScope("Sharable");
resource.setProperty("driverClassName", "com.mysql.jdbc.Driver");
resource.setProperty("url", "jdbc:mysql://localhost:3306/aqmtweb?autoreconnect=true");
resource.setProperty("username", "username");
resource.setProperty("password", "password");
ctx.getNamingResources().addResource(resource);

tomcat.start();

在Web应用程序本身中是这样的:

In the web application itself is this:

InitialContext ctx = new InitialContext();
// This line here throws the exception:
DataSource webds = (DataSource)ctx.lookup("java:/comp/env/jdbc/aqmtwebdb");

抛出的异常是:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at org.rand.aqmt.UserBean.<init>(UserBean.java:77)

[其中UserBean.java:77行是上面的ctx.lookup()]

[where line UserBean.java:77 is the ctx.lookup() above]

我对如何前进感到茫然.

I'm fairly at a loss as to how to progress.

推荐答案

很明显,我只需要

Evidently I just needed Tomcat.enableNaming(), which isn't enabled by default.

这篇关于嵌入的Tomcat:尝试获取数据源时出现NoInitialContextException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆