Thursday, October 15, 2009

GWT 1.7 (Hosted Mode): Using a data source

Spring Data Source:
Typically your Spring configuration files specify a data source that is used by your persistence layer to access the physical database.

In order to avoid having to keep changing the data source's details per environment in the Spring config files (dev, test, qa, prod), the easy way is to use a JNDI look up for your data source. Each environment will then be responsible for having the correct data source set up for use.

From the Spring configuration file:
<!-- The data source which is looked up via JNDI -->
  <jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyApp" lookup-on-startup="true" />

Data Source in web application
In the web.xml file, define the data source for use:
<!-- The data source that the application uses to access the current database -->
  <resource-ref>
        <description>The Oracle database data source.</description>
        <res-ref-name>jdbc/MyApp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
  </resource-ref> 

GWT 1.7 (Hosted Mode):
Since GWT 1,7 uses Jetty to run in hosted mode, you need to set up a Jetty data source so that Spring has its data source to use when the application is run.

Create a file in the /war/WEB-INF folder called jetty-web.xml and define your data source in there:
<?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="MyApp" class="org.mortbay.jetty.plus.naming.Resource"> 
    <Arg>jdbc/MyApp</Arg>
    <Arg>
      <New class="oracle.jdbc.pool.OracleDataSource">
        <Set name="user">mast</Set>
        <Set name="password">mast</Set>
        <Set name="URL">jdbc:oracle:thin:@localhost:1521:XE</Set>      
        <Set name="connectionCachingEnabled">true</Set> 
     </New>
    </Arg>
  </New>
</Configure>

Running the GWT Application (Eclipse using Google's GWT Plug-in):
You need to add two Jetty jars to your Eclipse's project classpath, namely:
jetty-name-6.1.x.jar and jetty-plus-6.1.x.jar.
I downloaded Jetty 6.1.19 and used the jars from it.

When you run the application (Right-click the project > Run As > Web Application), you need to modify the Run Configuration slightly.
To modify an existing instance of the application's Run Configuration, use Eclipse's menu option: Run > Run Configurations and select the correct GWT application instance.

On the Arguments tab, add the following VM argument:
-Djava.naming.factory.initial=org.mortbay.naming.InitialContextFactory

Now when you run the application in GWT hosted mode, Spring should have no trouble finding and using the specified data source.

Standalone Mode
Obviously you can do a similar set up for standlone mode (Tomcat or whatever web server you are using) - define the data source in the appropriate way so it is available for the application to use.

Note: the jetty-web.xml file obviously does not need to be deployed with the application into another web container such as Tomcat.

No comments:

Post a Comment