当前位置: 动力学知识库 > 问答 > 编程问答 >

maven jetty plugin - Java SQL DataSource Resource Configuration

问题描述:

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">...

网友答案:

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 ?

分享给朋友:
您可能感兴趣的文章:
随机阅读: