2012-03-14 66 views
3

對不起,如果這個問題似乎是重複的,但我無法從現有帖子的搜索結果中閃現出可行的解決方案。如何在Eclipse中設置GWT/Jetty連接池?

我有一個使用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" 
     /> 

所有這一切都工作得很好,當戰爭文件部署到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 

我試圖用碼頭-env.xml在戰爭中的WEB-INF,但沒有工作,要麼(同錯誤),可能是jetty-env.xml或其他語法中的語法錯誤。

這裏是我的碼頭-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> 

最後一兩件事,如果我改變了碼頭-env.xml到碼頭-web.xml中的名稱,然後在我得到一個503 HTML錯誤時我嘗試連接瀏覽器。 (eclipse顯示以下錯誤: [WARN]上下文啓動失敗com.g[email protected]111a20c {/,E:\ dev \ src \ servers \ webservice \ war} java .lang.ClassNotFoundException:org.eclipse.jetty.server.Server)

所以我認爲jetty-env沒有加載並且jetty-web是,但是我的jetty-web顯然干擾了GWT的jetty設置。

回答

2

你缺少org.eclipse.jetty.server.Server,但我認爲這是不對的類。 GWT 2.5.1使用碼頭6.1.11並根據thisorg.eclipse.jetty包裹從Jetty 7,而不是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> 

沒有行家可以從網站上下載jetty-namingjetty-plus罐子。

我的資源是PostgreSQL JDBC驅動程序。現在一切正常 - 在託管模式下是從jetty-web.xml加載的資源配置。當我在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> 

碼頭-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

感謝您的詳細解答!我嘗試了它,它不適合我。我相信那是因爲我使用了一個更新的GWT(2.6.0),我相信這是使用Jetty 8.1.12,基於這個SO回答(https://stackoverflow.com/a/17592318)。我相信配置文件和類名稱會有所不同,但我無法在線查找Jetty 8文檔(只有Jetty 9.3.x可用)。無論如何,我將嘗試遵循Jetty 9的指令(這裏:https://www.eclipse.org/jetty/documentation/9.3.x/using-jetty-jndi.html)。如果您有任何見解,請與我分享。 – leeyuiwah 2017-11-12 23:20:50