首页 文章

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

提问于
浏览
3

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

我有一个使用JDBC连接池的Web应用程序 . 连接池资源在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中的语法错误或其他东西 .

这是我的码头环境:

<?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,但是我的码头网明显干扰了GWT的码头设置 .

2 回答

  • 2

    你缺少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获取的 .

    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>
    

    码头-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");
    
  • 0

    虽然我不熟悉您的jetty问题,但我知道您可以使用-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

相关问题