<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7134711826265180323</id><updated>2011-11-27T15:41:28.032-08:00</updated><category term='JNDI'/><category term='GWT'/><category term='JDBC'/><category term='DataSource'/><title type='text'>Humble Code</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://humblecode.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7134711826265180323/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://humblecode.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Chad Skinner</name><uri>http://www.blogger.com/profile/12933400997156334341</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7134711826265180323.post-333110980597159604</id><published>2009-05-06T08:32:00.000-07:00</published><updated>2009-05-07T09:15:28.523-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='DataSource'/><category scheme='http://www.blogger.com/atom/ns#' term='JNDI'/><category scheme='http://www.blogger.com/atom/ns#' term='GWT'/><category scheme='http://www.blogger.com/atom/ns#' term='JDBC'/><title type='text'>GWT 1.6 - Using a JNDI Datasource</title><content type='html'>With the update to GWT 1.6 we found that our JNDI connections would no longer work. This was due to GWT 1.6 using Jetty instead of Tomcat as the embedded server. After searching online we found very few documents or posts explaining how to use a JNDI DataSource with GWT. The problem with most of these methods was that they required you to rewrite some of the Java code. Continuing to research Jetty the following solution was found that requires a few simple modifications to the project.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Configure the DataSource:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;To do this you will need to configure a jetty-web.xml file and place it in your Web Application’s WEB-INF directory. Details on how to do this for various databases are located on the Jetty Documentation site at &lt;a href="http://docs.codehaus.org/display/JETTY/DataSource+Examples"&gt;http://docs.codehaus.org/display/JETTY/DataSource+Examples&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;NOTE:  I believe the version of jetty shipped with GWT is below 6.1.12 and therefore you must leave off the first parameter in the example docs as it was added in jetty 6.1.12rc3. See the note at the top of the Jetty documents page.&lt;br /&gt;&lt;br /&gt;A Sample jetty-web.xml file for connecting to a local mysql server follows:&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;xml&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;version&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”1.0”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;?&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;DOCTYPE&lt;/span&gt; &lt;span style="color: rgb(0,128,128);"&gt;Configure&lt;/span&gt; &lt;span style="color: rgb(128,128,128);"&gt;PUBLIC&lt;/span&gt; &lt;span style="color: rgb(0,128,128);"&gt;“-//Mort Bay Consulting//DTD Configure//EN”&lt;/span&gt; &lt;span style="color: rgb(63,127,95);"&gt;“&lt;/span&gt;&lt;a href="http://jetty.mortbay.org/configure.dtd"&gt;http://jetty.mortbay.org/configure.dtd&lt;/a&gt;&lt;span style="color: rgb(63,127,95);"&gt;”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Configure&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;class&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”org.mortbay.jetty.webapp.WebAppContext”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;New&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;id&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”website”&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;class&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”org.mortbay.jetty.plus.naming.Resource”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Arg&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;java:comp/&lt;span style="text-decoration: underline;"&gt;env&lt;/span&gt;/&lt;span style="text-decoration: underline;"&gt;jdbc&lt;/span&gt;/&lt;span style="text-decoration: underline;"&gt;database&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Arg&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Arg&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;New&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;class&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                                &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Set&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;name&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”Url”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;a href="jdbc:mysql://localhost:3306/website"&gt;jdbc:mysql://&lt;span style="text-decoration: underline;"&gt;localhost&lt;/span&gt;:3306/&lt;/a&gt;&lt;span style="text-decoration: underline;"&gt;database&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Set&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                                &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Set&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;name&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”User”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(127,0,0);"&gt;&lt;em&gt;&lt;strong&gt;(Username)&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Set&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                                &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Set&lt;/span&gt; &lt;span style="color: rgb(127,0,127);"&gt;name&lt;/span&gt;=&lt;span style="color: rgb(42,0,255);"&gt;”Password”&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: rgb(127,0,0);"&gt;&lt;em&gt;&lt;strong&gt;(Password)&lt;/strong&gt;&lt;/em&gt;&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Set&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                        &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;New&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;                &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Arg&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;New&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(63,127,127);"&gt;Configure&lt;/span&gt;&lt;span style="color: rgb(0,128,128);"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;strong&gt;Configure the Eclipse Run Configuration:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Open the Eclipse run configuration and select the “Arguments” Tab. Then under “VM arguments“ section add the following:&lt;br /&gt;&lt;br /&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;br /&gt;-Djava.naming.factory.initial=org.mortbay.naming.InitialContextFactory&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Acquire a Database Connection:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;You should now be able to make a JNDI lookup to retrieve the datasource:&lt;br /&gt;&lt;pre class="code"&gt;&lt;code&gt;&lt;br /&gt;&lt;span style="color: rgb(127,0,85);"&gt;public&lt;/span&gt; Connection retrieveConnection() &lt;span style="color: rgb(127,0,85);"&gt;throws&lt;/span&gt; Exception {&lt;br /&gt;        Connection lConnection = &lt;span style="color: rgb(127,0,85);"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;        Context lContext = &lt;span style="color: rgb(127,0,85);"&gt;new&lt;/span&gt; InitialContext();&lt;br /&gt;        DataSource lDataSource = (DataSource) lContext.lookup(&lt;span style="color: rgb(42,0,255);"&gt;"java:comp/env/jdbc/database"&lt;/span&gt;);&lt;br /&gt;        lConnection = lDataSource.getConnection();&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: rgb(127,0,85);"&gt;return&lt;/span&gt; lConnection;                &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Resource Injection:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Supposedly Jetty supports the servlet 2.5 specification and resource injection via the web.xml entry or @resource annotation. However, I have yet to figure out if this is supported by the Jetty version shipped with GWT. If anyone has figured out whether or not this works and if so how it is done please let me know.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7134711826265180323-333110980597159604?l=humblecode.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://humblecode.blogspot.com/feeds/333110980597159604/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7134711826265180323&amp;postID=333110980597159604' title='38 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7134711826265180323/posts/default/333110980597159604'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7134711826265180323/posts/default/333110980597159604'/><link rel='alternate' type='text/html' href='http://humblecode.blogspot.com/2009/05/gwt-16-using-jndi-datasource.html' title='GWT 1.6 - Using a JNDI Datasource'/><author><name>Chad Skinner</name><uri>http://www.blogger.com/profile/12933400997156334341</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>38</thr:total></entry></feed>
