【发布时间】:2012-06-03 18:45:10
【问题描述】:
我有一个非常简单的带有 maven 和 jetty 的 webapp 项目,到目前为止一直运行良好。但是现在我需要使用 JNDI 设置 MySQL 连接池,因为数据库连接总是超时。
这里首先是我的pom.xml的相关内容:
<modelVersion>4.0.0</modelVersion>
...
<packaging>war</packaging>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty-version>8.1.0.v20120127</jetty-version>
</properties>
<dependencies>
...
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.20</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
...
</build>
现在我在文件夹 /src/main/webapp/WEB-INF 中创建了一个 jetty-env.xml,内容如下:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="project-db" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/db</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="url">jdbc:mysql://www.example.com:3306/mydb</Set>
<Set name="username">dbuser</Set>
<Set name="password">dbpass</Set>
</New>
</Arg>
</New>
</Configure>
但问题是我什至无法测试此连接是否有效,因为 jetty-maven-plugin 无法在目标上启动
mvn jetty:run
出现以下错误:
WARN:oejw.WebAppContext:Failed startup of context o.m.j.p.JettyWebAppContext
{/,file:/D:/documents/programmierung/workspace/battleships-trunk/src/main/webapp/}
,file:/D:/documents/programmierung/workspace/battleships-trunk/src/main/webapp/
java.lang.IllegalArgumentException: Object of class
'org.mortbay.jetty.plugin.JettyWebAppContext' is not of type
'org.eclipse.jetty.webapp.WebAppContext'.
Object Class and type Class are from different loaders.
那么我怎样才能让它工作呢?我不得不使用 Jetty 8.x 版,因为我需要 WebSocket 支持,并且远程生产服务器将运行 Jetty 8。
编辑 在 Pavel Veller 回答之前,我尝试了以下操作:将组装好的战争部署到远程 jetty8 服务器并得到相同的错误,只是之前的错误现在如下所示:
java.lang.IllegalArgumentException: Object of class
'org.eclipse.jetty.webapp.WebAppContext' is not of type
'org.eclipse.jetty.webapp.WebAppContext'.
Object Class and type Class are from different loaders.
所以看起来好像有多个类加载器发生冲突。
EDIT2 按照 Pavel 的要求,我使用简化的 webapp 重新创建了错误,您可以找到 here on Dropbox。这是一个压缩的 Eclipse Maven 项目。
【问题讨论】:
-
这个问题与数据源无关。我根本没有 jetty-env.xml。
标签: mysql jetty jndi maven-jetty-plugin