adaj21
adaj21

Reputation: 573

How do I setup Connection Pool with GWT/Jetty in eclipse?

Excuse me if this question seems repetitive, but I was unable to gleam a workable solution from the search results of existing posts.

I have a web app that uses a JDBC connection pool. The connection pool resource is defined in the war file's meta-inf/context.xml. When I deploy the war file to tomcat, this file gets copied (and renamed to match my war file's name) to tomcat's conf/catalina/localhost folder. My code gets the connection's datasource object like this:

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

and my context.xml has the following resource defined:

<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"
      />

All of this works well when the war file is deployed to tomcat.

However, during development in eclipse, and because of GWT dependencies, I often need to debug using GWT's built in jetty container.

When I do this, the InitialContext() call fails with the following message:

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

I tried to use jetty-env.xml in the war's web-inf but that did not work either (same error), perhaps a syntax error in the jetty-env.xml or something else.

Here is my 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>

One last thing, If I changed the name of jetty-env.xml to jetty-web.xml, then the the I get a 503 HTML error when I try to connect the browser. (eclipse showes the following error: [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)

So I believe jetty-env was not loaded and jetty-web is, but my jetty-web obviously interferes with GWT's settings for jetty.

Upvotes: 3

Views: 4140

Answers (2)

rzehan
rzehan

Reputation: 658

You are missing org.eclipse.jetty.server.Server, but I think it is not right class. GWT 2.5.1 uses jetty 6.1.11 and according to this is org.eclipse.jetty package from Jetty 7, not 6!

I had similar problem and I solved it by adding libraries jetty-naming and jetty-plus to my project. It is maven project so I added this to 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>

Without maven you can download jetty-naming and jetty-plus jars from web.

My resource is PostgreSQL JDBC driver. Now everything works - in hosted mode is resource configuration loaded from jetty-web.xml. And when I build war and deploy in Tomcat, the resource is obtained from 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>

Also my web.xml contains this reference:

<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 is loaded this way (simplified):

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

Upvotes: 2

Stevko
Stevko

Reputation: 4495

While I am not familiar with your jetty issue, I do know that you can use your own servlet container instead of GWT's servlet container by using the -noserver flag. It works great and allows me to use any other server while in hosted mode.

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

Upvotes: 0

Related Questions