We’re now using GWT 1.6 at my work and we wanted to configure our applications to use JNDI. Although there are a few blog posts on this topic, each left out a bit of information that would have been helpful in getting the system to work. So I decided to put up a post here that goes step-by-step through my installation to help others who might have the same issues I did. Where ever possible, I will also reference the original posts I had found, in case you’d like to look there.
My setup
First things first – here are the tools I’m using
- Eclipse Ganymede with GWT Plugin
- GWT 1.6
- JTDS 1.2.2
Steps
1. Create jetty-web.xml file in war/WEB-INF/
2. Copy the following into jetty-web.xml
<?xml version=”1.0″?>
<!DOCTYPE Configure PUBLIC “-//Mort Bay Consulting//DTD Configure//EN” “http://jetty.mortbay.org/configure.dtd”>
<Configure class=”org.mortbay.jetty.webapp.WebAppContext”>
<New id=”website” class=”org.mortbay.jetty.plus.naming.Resource”>
<Arg>java:comp/env/jdbc/dataSourceName</Arg>
<Arg>
<New class=”net.sourceforge.jtds.jdbcx.JtdsDataSource”>
<Set name=”DatabaseName”>(databaseName)</Set>
<Set name=”ServerName”>(serverName)</Set>
<Set name=”User”>(User)</Set>
<Set name=”Password”>(Password)</Set>
</New>
</Arg>
</New>
</Configure>
3. Add Jetty Plus and Jetty Naming to your classpath
For this step, I created a new folder off the root of my GWT project called, “dependency”. Please note that jetty-naming has a dependency on activation and mail. So, the .jar files I have to import into my project are:
- jetty-plus-6.1.12.jar
- jetty-naming-6.1.12.jar
- activation-1.1.jar
- mail-1.4.jar
I want to deploy these applications to Tomcat, so I configured the launcher to include these on the classpath, Run->Run Configurations…->Classpath->User Entries->Add JARS…
While configuring the laucher, add the following under Arguments->VM Arguments
-Djava.naming.factory.initial=org.mortbay.naming.InitialContextFactory
4. In my code, I now get the connection with the following:
Context lContext = new InitialContext();
DataSource lDataSource = (DataSource) lContext.lookup(“java:comp/env/jdbc/dataSourceName”);
Connection connection = lDataSource.getConnection();
Alternate
If you want to use connection pooling or a different database, here is a link to the Jetty page that shows some sample configs: http://docs.codehaus.org/display/JETTY/DataSource+Examples
The most informative BLOG entry I found: http://humblecode.blogspot.com/2009/05/gwt-16-using-jndi-datasource.html
Tags: GWT, Java, JNDI, Programming
June 30, 2009 at 8:47 pm |
Nice job. This has been plaguing me for days and I wasn’t able to fix it until I saw this post. Thanks a bunch.