polypiel
polypiel

Reputation: 2341

Defining two data sources in jetty (jetty-env.xml)

I'm trying to define two data sources in my web application, using the jetty-env.xml file. It works ok with just one data source, however I get this exception when the second data source is added:

java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default

Here's my configuration:

jetty-env.xml

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="ds" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>jdbc/mybd1</Arg>
        <Arg>
            <New class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <Set name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
                 <Set name="jdbcUrl">jdbc:jtds:sqlserver://url:1433/mybd1</Set>
                 <Set name="user">xx</Set>
                 <Set name="password">yy</Set>
            </New>
        </Arg>
    </New>

    <New id="ds2" class="org.eclipse.jetty.plus.jndi.Resource" >
        <Arg>jdbc/mybd2</Arg>
        <Arg>
            <New class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <Set name="driverClass">com.microsoft.sqlserver.jdbc.SQLServerDriver</Set>
                <Set name="jdbcUrl">jdbc:jtds:sqlserver://url:1433/mybd2</Set>
                <Set name="user">xx</Set>
                <Set name="password">yy</Set>
            </New>
        </Arg>
    </New>
</Configure> 

web.xml

<resource-ref>
    <res-ref-name>jdbc/mybd1</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
    <res-ref-name>jdbc/mybd2</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

hibernate.cfg.xml (there is another hibernate.cfb.xml to configure the second data source)

<session-factory>
  <property name="connection.datasource">jdbc/mybd1</property>
  <!-- ... -->

Any clue?

Upvotes: 9

Views: 15371

Answers (4)

javierdotnet
javierdotnet

Reputation: 36

Take a look in : https://www.eclipse.org/jetty/documentation/9.4.x/using-jetty-jndi.html

Deciding Where to Declare Resources You can define naming resources in three places:

jetty.xml Naming resources defined in a jetty.xml file are scoped at either the JVM level or the Server level. The classes for the resource must be visible at the Jetty container level. If the classes for the resource only exist inside your webapp, you must declare it in a WEB-INF/jetty-env.xml file.

WEB-INF/jetty-env.xml Naming resources in a WEB-INF/jetty-env.xml file are scoped to the web app in which the file resides. While you can enter JVM or Server scopes if you choose, we do not recommend doing so. The resources defined here may use classes from inside your webapp. This is a Jetty-specific mechanism.

Context xml file Entries in a context xml file should be scoped at the level of the webapp to which they apply, although you can supply a less strict scoping level of Server or JVM if you choose. As with resources declared in a jetty.xml file, classes associated with the resource must be visible on the container’s classpath.

And put a file like this :

     <?xml version="1.0"?>
 <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

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

   <!-- Add an EnvEntry only valid for this webapp               -->
   <New id="gargle"  class="org.eclipse.jetty.plus.jndi.EnvEntry">
     <Arg>gargle</Arg>
     <Arg type="java.lang.Double">100</Arg>
     <Arg type="boolean">true</Arg>
   </New>

  <!-- Add an override for a global EnvEntry                           -->
   <New id="wiggle"  class="org.eclipse.jetty.plus.jndi.EnvEntry">
     <Arg>wiggle</Arg>
     <Arg type="java.lang.Double">55.0</Arg>
     <Arg type="boolean">true</Arg>
   </New>

   <!-- an XADataSource                                                -->
   <New id="mydatasource99" class="org.eclipse.jetty.plus.jndi.Resource">
     <Arg>jdbc/mydatasource99</Arg>
     <Arg>
       <New class="com.atomikos.jdbc.SimpleDataSourceBean">
         <Set name="xaDataSourceClassName">org.apache.derby.jdbc.EmbeddedXADataSource</Set>
         <Set name="xaDataSourceProperties">databaseName=testdb99;createDatabase=create</Set>
         <Set name="UniqueResourceName">mydatasource99</Set>
       </New>
     </Arg>
   </New>

 </Configure>

Upvotes: 1

janih
janih

Reputation: 2234

The id parameter values should match in jetty-env.xml and web.xml

jetty-env.xml

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="DS1" class="org.eclipse.jetty.plus.jndi.Resource">...</New>
    <New id="DS2" class="org.eclipse.jetty.plus.jndi.Resource">...</New>
</Configure> 

web.xml

<resource-ref id="DS1">...</resource-ref>
<resource-ref id="DS2">...</resource-ref>

Upvotes: 2

vlk32
vlk32

Reputation: 59

Try to enable logging in Jetty. Be carefull logger name is "jndi". Jetty developers don't use class-name as a logger-name for JNDI.

I spent 2 days to finding difference between name defined in web.xml and jetty-env.xml.

Upvotes: 1

Tim
Tim

Reputation: 6519

I haven't had a chance to test it, but it looks to me like your problem is that you're missing an <Arg /> for the scope.

Your DS should be:

    <New id="ds" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg></Arg>
    <Arg>jdbc/mybd1</Arg>
    <Arg>
        <New class="com.mchange.v2.c3p0.ComboPooledDataSource">

etc.

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

Upvotes: 4

Related Questions