【发布时间】:2015-10-07 23:32:56
【问题描述】:
我有一个这样的示例代码:
ConnectionPool.dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/murach");
在 webapp/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/murach"
auth="Container"
type="javax.sql.DataSource"
username="root"
--- Rest of the text ---/>
</Context>
当我将此 Web 应用程序部署到 Tomcat 时,数据库连接很好,但是当我尝试使用 Jetty 插件使用 Jetty 时:jetty:run-war
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.1.v20140609</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<httpConnector>
<port>8082</port>
</httpConnector>
<webApp>
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
我得到:
javax.naming.NameNotFoundException; remaining name 'jdbc/murach'
我怎样才能使这项工作也与码头一起工作?
我也试过添加
<resource-ref>
<description>murach</description>
<res-ref-name>jdbc/murach</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
到 web.xml,但现在我得到:
java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default
【问题讨论】:
-
这个数据源是为你的 Jetty 服务器定义的吗? https://wiki.eclipse.org/Jetty/Howto/Configure_JNDI_Datasource
-
@Koray Tugay 请参阅下面的 m.hassaballah 的回答。您将在 web.xml 文件中将数据源定义为资源,而不仅仅是在 Context.xml 中。
-
只是想知道...我的回答是否有帮助或者使用建议的方法是否存在一些问题?
标签: java maven tomcat jetty jndi