-1

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.

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.

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.

For example, this answer 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.)

I have the a Resource entry in server.xml like so:

<Context path="/" docBase="/var/lib/tomcat7/webapps/myapp" reloadable="true">
    <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" />
</Context>

jetty-maven-plugin POM entry:

...
<plugins>
    <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.14.v20131031</version>
        <dependencies>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.29</version>
            </dependency>           
        </dependencies>         
        <configuration>
            <!-- <useProvidedScope>?</useProvidedScope> -->
            <webApp>
                <jettyEnvXml>${basedir}/jetty-env.xml</jettyEnvXml>             
            </webApp>
        </configuration>
</plugin>   
...

This works fine in Tomcat7.

For Jetty, I am using jetty-env.xml with the following configuration:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="ds" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>mysql-dataSource</Arg>
        <Arg>
            <New class="javax.sql.DataSource">
                <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
                <Set name="url">jdbc:mysql://localhost:3306/dbname</Set>
                <Set name="username">dbuser</Set>
                <Set name="password">dbuser</Set>
            </New>
        </Arg>
    </New>
</Configure>

With that I get this error:

java.lang.IllegalStateException: No Constructor: <New class="javax.sql.DataSource">...
Community
  • 1
  • 1
Shroder
  • 573
  • 2
  • 5
  • 23
  • 1
    I have reworded the question so that it is more focused on what I am really asking. That being, a one-to-one translation between Tomcat and Jetty configs. The answer indicated as being the same as this uses different classes (EnvEntry vs Resource, MysqlConnectionPoolDataSource vs DataSource). – Shroder Jun 29 '14 at 17:17

1 Answers1

1

Add this

 <Arg></Arg>

above

<Arg>mysql-dataSource</Arg>

As first "Arg" is the scope, and without it, the rest of your arguments are out of position, and are probably causing your issue.

For more information Jetty/Howto/Configure JNDI Datasource

Edit :

Have you add scope provided for the specific version of Jetty ?

OO7
  • 2,785
  • 1
  • 21
  • 33
  • Are you referring to "useProvidedScope"? I've tried it with True and False after adding the missing with no luck. I'm going to update my question with my POM configuration. – Shroder Jun 29 '14 at 15:53