How to setup connection pool with GWT / Jetty in eclipse?
Sorry if this question seems repetitive, but I was unable to clarify a workable solution from the search results for existing posts.
I have a web application that uses a JDBC connection pool. The connection pool resource is defined in the war file of the meta-inf / context.xml file. When I deploy the war file to tomcat, this file is copied (and renamed to match the name of my war file) to the tomcat conf / catalina / localhost folder. My code gets the connection 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:
<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"
/>
This all works well when the war file is deployed to tomcat.
However, during development in eclipse and due to GWT dependencies, I often have to debug using the GWT embedded in the jetty container.
When I do this, the InitialContext () call ends with the 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 using jetty-env.xml in war web-inf but that didn't work either (same error), maybe syntax error in jetty-env.xml file or something.
Here is my pier:
<?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>
Lastly, if I change the name of jetty-env.xml to jetty-web.xml, then I get a 503 HTML error when I try to connect the browser. (eclipse shows the following error: [WARN] Failed to start context com.g oogle.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 but jetty-net, but my jetty is clearly interfering with the jetty-env settings for jetty.
source to share
You are missing org.eclipse.jetty.server.Server, but I think it is not the correct class. GWT 2.5.1 uses jetty 6.1.11 and according to that is the org.eclipse.jetty package from Jetty 7, not 6!
I had a similar problem and solved it by adding the jetty-naming and jetty-plus libraries project to the project. This is a maven project, so I added it to the 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 banks from the internet.
My resource is PostgreSQL JDBC driver. Everything now works - in host mode - the resource config loaded from jetty-web.xml. And when I create a war and deploy to 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 link:
<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>
The data source is loaded like this (simplified):
DataSource dataSource = (DataSource) new InitialContext().lookup("java:/comp/env/jdbc/postgres");
source to share