Wednesday, June 22, 2011

GWT & Databases

UPDATE For GWT 2.6+:
You will need to reference a few more jars in your project in order for jetty to work:


You will also need to place a "jetty-web.xml" file in your WEB-INF directory.  The contents of this file will be used to create the pool in jetty.

The file contents are given below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

        <New id="website" class="org.eclipse.jetty.plus.jndi.Resource">

                <Arg></Arg>
                <Arg>java:comp/env/jdbc/poolname</Arg>

<Arg>
                        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
                                <Set name="Url">jdbc:mysql://hostname:3306/dbname</Set>
                                <Set name="User">username</Set>
                                <Set name="Password">password</Set>
                        </New>
                </Arg>
        </New>

</Configure>

There is one last thing to setup, and that is your Run/Debug configuration. In the VM arguments you will need to add:
-Djava.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory

For GWT 2.5:

If you plan on running your GWT application on tomcat, you setup your datasource in the usual way. Normally you use a connection pool, which you setup in META-INF/context.xml

By default, GWT uses jetty when you are running your application in hosted mode.  In order to use the same code to access the database, you must configure a few things in your project.

You will need to reference a few more jars in your project in order for jetty to work:




You will also need to place a "jetty-web.xml" file in your WEB-INF directory.  The contents of this file will be used to create the pool in jetty.
The file contents are given below:

<?xml version="1.0" encoding="UTF-8"?>
<!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/poolname</Arg>
                <Arg>
                        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
                                <Set name="Url">jdbc:mysql://hostname:3306/dbname</Set>
                                <Set name="User">username</Set>
                                <Set name="Password">password</Set>
                        </New>
                </Arg>
        </New>

</Configure>


There is one last thing to setup, and that is your Run/Debug configuration. In the VM arguments you will need to add:
-Djava.naming.factory.initial=org.mortbay.naming.InitialContextFactory
as shown below:

5 comments:

Anonymous said...

Many thanks.

Anonymous said...

I really liked the article, and the very cool blog

Anonymous said...

brinkka2011 says: Im struggling to get a way to make contact with you through your website, do you not have a contact form on the website at all?

Scott Harmon said...

@brinkka2011 See http://people.cis.ksu.edu/~harmon/

Anonymous said...

brinkka2011 says: Resources such as the 1 you mentioned right here will be extremely useful to myself! I will publish a hyperlink to this web page on my personalized blog. Im sure my site site visitors will locate that fairly advantageous.