【发布时间】:2014-10-06 13:01:24
【问题描述】:
我正在使用带有标准 MySQL 连接器 API 的 Jetty 9.2(嵌入式),我对应该如何设置感到非常困惑。目前,我的 web.xml 文件中有这个:
<webapp ...
<resource-ref>
<description>JDBC Data Source</description>
<res-ref-name>jdbc/DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</webapp>
...这在我的 jetty-env.xml 中:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DatabaseConnector" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/DataSource</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://localhost:3306/DBName</Set>
<Set name="User">user</Set>
<Set name="Password">pass</Set>
</New>
</Arg>
</New>
</Configure>
...以及要初始化的这段代码:
Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
DataSource datasource = (DataSource) envCtx.lookup("jdbc/DataSource");
当我尝试启动服务器时,我收到错误 javax.naming.NameNotFoundException; remaining name 'jdbc/DataSource'。我在代码初始化中尝试了许多不同的字符串变体,例如删除InitialContext 对象上的lookup 调用,但我只是不断收到具有不同name 值的相同错误的变体;
这两个 xml 文件都位于我的 /WAR/WEB-INF 目录中。我查看了大量以前的问题和教程、博客等,但我无处可去。
【问题讨论】:
-
您能否发布您的 Jetty Embedded 实例的
WebAppContext.setConfiguration()代码? -
我得到了我在其他地方寻找的答案,我会发布一个总结解决方案的答案。
标签: java jdbc jetty jndi embedded-jetty