Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 903
polypiel Avatar asked Jul 04 '12 12:07

polypiel


3 Answers

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.

like image 149
Tim Avatar answered Nov 07 '22 05:11

Tim


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>
like image 40
janih Avatar answered Nov 07 '22 03:11

janih


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.

like image 38
vlk32 Avatar answered Nov 07 '22 03:11

vlk32