如何在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中的语法错误或其他问题。

这是我的jetty-env:

<?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] Failed startup of context 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的设置。
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加载。当我构建war并在Tomcat中部署时,资源是从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答案(https://dev59.com/QnTYa4cB1Zd3GeqPtlcC#17592318)。我相信配置文件和类名将会不同,但我无法在网上找到Jetty 8的文档(只有Jetty 9.3.x可用)。无论如何,我将尝试按照Jetty 9的说明进行操作(这里:https://www.eclipse.org/jetty/documentation/9.3.x/using-jetty-jndi.html)。如果您有任何见解,请与我分享。 - leeyuiwah

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接