【问题标题】:How do I setup Connection Pool with GWT/Jetty in eclipse?如何在 Eclipse 中使用 GWT/Jetty 设置连接池?
【发布时间】:2012-03-14 17:06:42
【问题描述】:

对不起,如果这个问题似乎重复,但我无法从现有帖子的搜索结果中找到可行的解决方案。

我有一个使用 JDBC 连接池的 Web 应用程序。连接池资源在war 文件的meta-inf/context.xml 中定义。当我将战争文件部署到 tomcat 时,该文件被复制(并重命名以匹配我的战争文件的名称)到 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

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

这是我的码头环境:

<?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 设置。

【问题讨论】:

    标签: eclipse gwt jdbc jetty jndi


    【解决方案1】:

    您缺少 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,您可以从 web 下载 jetty-namingjetty-plus jar。

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

    context.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");
    

    【讨论】:

    • 感谢您的详细解答!我试过了,它对我不起作用。我相信这是因为我使用的是更新的 GWT (2.6.0),我相信它使用的是 Jetty 8.1.12,基于这个 SO 答案 (stackoverflow.com/a/17592318)。我相信配置文件和类名会有所不同,但是我在网上找不到 Jetty 8 文档(只有 Jetty 9.3.x 可用)。无论如何,我将尝试按照 Jetty 9 的说明进行操作(此处:eclipse.org/jetty/documentation/9.3.x/using-jetty-jndi.html)。如果您有任何见解,请与我分享。
    【解决方案2】:

    虽然我不熟悉您的码头问题,但我知道您可以通过使用 -noserver 标志来使用自己的 servlet 容器而不是 GWT 的 servlet 容器。它运行良好,允许我在托管模式下使用任何其他服务器。

    http://code.google.com/webtoolkit/doc/latest/DevGuideCompilingAndDebugging.html#How_do_I_use_my_own_server_in_development_mode_instead_of_GWT's

    【讨论】:

      猜你喜欢
      • 2012-10-30
      • 2016-01-30
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2019-03-15
      • 1970-01-01
      相关资源
      最近更新 更多