title: Java SQL DataSource Resource Configuration
body: <p>In Java Spring, I am trying to keep my database settings outside of the main app so that it can be configured on other servers with minimal effort. I am using Tomcat 7 for my production server and a Jetty server (jetty-maven-plugin version 8.1.14.v20131031) for development.</p>
  
  <p>The main problem I am running into is I cannot get the working Tomcat 7 configuration to translate to Jetty using the same class (javax.sql.DataSource). I am starting with this class, opposed to a more robust one so that I am starting with something as basic as possible.</p>
  
  <p>If this is a case where there is no direct translation between the two servers, or this is simply bad practice I am looking for what would be a good practice or standard.</p>
  
  <p>For example, <a href="https://stackoverflow.com/questions/7923980/jetty-7-configuring-jndi-for-start-java">this answer</a> is an alternative configuration that uses a different class, and works with Jetty. (However it doesn't answer why the basic DataSource class works with Tomcat and not with Jetty.)</p>
  
  <p>I have the a Resource entry in server.xml like so:</p>
  
  <pre><code>&lt;Context path="/" docBase="/var/lib/tomcat7/webapps/myapp" reloadable="true"&gt;
      &lt;Resource name="mysql-dataSource" auth="Container" type="javax.sql.DataSource"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/dbname"
            username="dbuser"
            password="dbuser" /&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>jetty-maven-plugin POM entry:</p>
  
  <pre><code>...
  &lt;plugins&gt;
      &lt;plugin&gt;
          &lt;groupId&gt;org.mortbay.jetty&lt;/groupId&gt;
          &lt;artifactId&gt;jetty-maven-plugin&lt;/artifactId&gt;
          &lt;version&gt;8.1.14.v20131031&lt;/version&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;mysql&lt;/groupId&gt;
                  &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
                  &lt;version&gt;5.1.29&lt;/version&gt;
              &lt;/dependency&gt;           
          &lt;/dependencies&gt;         
          &lt;configuration&gt;
              &lt;!-- &lt;useProvidedScope&gt;?&lt;/useProvidedScope&gt; --&gt;
              &lt;webApp&gt;
                  &lt;jettyEnvXml&gt;${basedir}/jetty-env.xml&lt;/jettyEnvXml&gt;             
              &lt;/webApp&gt;
          &lt;/configuration&gt;
  &lt;/plugin&gt;   
  ...
  </code></pre>
  
  <p>This works fine in Tomcat7. </p>
  
  <p>For Jetty, I am using jetty-env.xml with the following configuration:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd"&gt;
  &lt;Configure id="Server" class="org.eclipse.jetty.webapp.WebAppContext"&gt;
      &lt;New id="ds" class="org.eclipse.jetty.plus.jndi.Resource"&gt;
          &lt;Arg&gt;mysql-dataSource&lt;/Arg&gt;
          &lt;Arg&gt;
              &lt;New class="javax.sql.DataSource"&gt;
                  &lt;Set name="driverClassName"&gt;com.mysql.jdbc.Driver&lt;/Set&gt;
                  &lt;Set name="url"&gt;jdbc:mysql://localhost:3306/dbname&lt;/Set&gt;
                  &lt;Set name="username"&gt;dbuser&lt;/Set&gt;
                  &lt;Set name="password"&gt;dbuser&lt;/Set&gt;
              &lt;/New&gt;
          &lt;/Arg&gt;
      &lt;/New&gt;
  &lt;/Configure&gt;
  </code></pre>
  
  <p>With that I get this error:</p>
  
  <pre><code>java.lang.IllegalStateException: No Constructor: &lt;New class="javax.sql.DataSource"&gt;...
  </code></pre>