eclipse - 如何在 Eclipse 中使用 GWT/Jetty 设置连接池?

标签 eclipse gwt jdbc jetty jndi

如果这个问题看起来重复,请原谅,但我无法从现有帖子的搜索结果中找到可行的解决方案。

我有一个使用 JDBC 连接池的网络应用程序。连接池资源在 war 文件的 meta-inf/context.xml 中定义。当我将 war 文件部署到 tomcat 时,该文件被复制(并重命名以匹配我的 war 文件的名称)到 tomcat 的 conf/catalina/localhost 文件夹。我的代码像这样获取连接的数据源对象:

Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
datasource = (DataSource) envCtx.lookup("jdbc/MYDB");

我的 context.xml 定义了以下资源:

<Resource name="jdbc/MYDB" 
      auth="Container" 
      type="javax.sql.DataSource"
      driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
      username="username" 
      password="password" 
      url="jdbc:sqlserver://myremote.database.server:1433;databaseName=testdb"
      />

当 war 文件部署到 tomcat 时,所有这些都运行良好。

但是,在使用 eclipse 进行开发期间,由于存在 GWT 依赖性,我经常需要使用 GWT 内置的 jetty 容器进行调试。

当我这样做时,InitialContext() 调用失败并显示以下消息:

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

我尝试在 war 的 web-inf 中使用 jetty-env.xml,但这也不起作用(同样的错误),可能是 jetty-env.xml 中的语法错误或其他原因。

这是我的 jetty 环境:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New id="OTMFTDB" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg></Arg>
    <Arg>jdbc/MYDB</Arg>
    <Arg>
        <New class="net.sourceforge.jtds.jdbcx.JtdsDataSource">
            <Set name="User">username</Set>
            <Set name="Password">password</Set>
            <Set name="DatabaseName">testdb</Set>
            <Set name="ServerName">myremote.database.server</Set>
            <Set name="PortNumber">1433</Set>
        </New>
    </Arg>
</New>
</Configure>

最后一件事,如果我将 jetty-env.xml 的名称更改为 jetty-web.xml,那么当我尝试连接浏览器时会收到 503 HTML 错误。 (eclipse 显示如下错误: [WARN] 上下文启动失败 com.google.gwt.dev.shell.jetty.JettyLauncher$WebAppContextWithReload@111a20c{/,E:\dev\src\servers\webservice\war} java.lang.ClassNotFoundException:org.eclipse.jetty.server.Server)

所以我相信 jetty-env 没有加载而 jetty-web 是,但我的 jetty-web 显然干扰了 GWT 的 jetty 设置。

最佳答案

您缺少 org.eclipse.jetty.server.Server,但我认为这不是正确的类。 GWT 2.5.1 使用 jetty 6.1.11 并根据 this是来自 Jetty 7 的 org.eclipse.jetty 包,而不是 6!

我有类似的问题,我通过在我的项目中添加库 jetty-naming 和 jetty-plus 来解决它。这是 Maven 项目,所以我将其添加到 pom.xml 中:

<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-naming</artifactId>
    <version>6.1.11</version>
</dependency>
<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty-plus</artifactId>
    <version>6.1.11</version>
</dependency>

没有maven你可以下载jetty-namingjetty-plus来自网络的 jar 。

我的资源是 PostgreSQL JDBC 驱动程序。现在一切正常 - 在托管模式下是从 jetty-web.xml 加载的资源配置。而我在Tomcat中构建war部署时,资源是从context.xml中获取的。

上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/web">
    <Resource auth="Container" driverClassName="org.postgresql.Driver"
        maxActive="100" maxIdle="30" maxWait="200" name="jdbc/postgres"
        username="testuser" password="testpass" type="javax.sql.DataSource"
        url="jdbc:postgresql://localhost:5433/resolver" />
</Context>

jetty -web.xml:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <New id="GWT_HOSTED_MODE_DB" class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>java:/comp/env/jdbc/postgres</Arg>
        <Arg>
            <New class="org.apache.commons.dbcp.BasicDataSource">
                <Set name="driverClassName">org.postgresql.Driver</Set>
                <Set name="url">jdbc:postgresql://localhost:5433/resolver</Set>
                <Set name="username">testuser</Set>
                <Set name="password">testpass</Set>
            </New>
        </Arg>
    </New>
</Configure>

我的 web.xml 也包含此引用:

<resource-ref>
    <description>Postgres Connection pool datasource</description>
    <res-ref-name>jdbc/postgres</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

数据源以这种方式加载(简化):

DataSource dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/postgres");

关于eclipse - 如何在 Eclipse 中使用 GWT/Jetty 设置连接池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9706626/

相关文章:

Android项目+Eclipse+注解处理

java - 在 Eclipse 等 IDE 中使用 JBoss 模块

java - 使用 SafeHtml 进行 anchor

eclipse - GWT 尝试加载已删除的模块

java - Oracle 和 Jdbc 以及 OraclePreparedStatement#setPlsqlIndexTable 和 java.util.Date

java - NamedParameterJdbcTemplate 与 JdbcTemplate

java - Ubuntu 12.04 上的 Eclipse 设置问题

eclipse - 我的 eclipse 不断崩溃

java - 建议 GWT 大规模应用服务器

java - 如何中断使用 Postgresql 运行 JDBC 查询的线程?