简体   繁体   English

在jetty中定义两个数据源(jetty-env.xml)

[英]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. 我正在尝试使用jetty-env.xml文件在我的Web应用程序中定义两个数据源。 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 码头-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 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) hibernate.cfg.xml (还有另一个hibernate.cfb.xml来配置第二个数据源)

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

Any clue? 任何线索?

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. 我没有机会测试它,但它看起来像你的问题是你错过了范围的<Arg />

Your DS should be: 您的DS应该是:

    <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. 第一个“Arg”是范围,没有它,你的其他论点都不合适,可能会导致你的问题。

The id parameter values should match in jetty-env.xml and web.xml id参数值应与jetty-env.xml和web.xml匹配

jetty-env.xml 码头-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 web.xml中

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

Try to enable logging in Jetty. 尝试在Jetty中启用日志记录。 Be carefull logger name is "jndi". 小心的记录器名称是“jndi”。 Jetty developers don't use class-name as a logger-name for JNDI. Jetty开发人员不使用class-name作为JNDI的logger-name。

I spent 2 days to finding difference between name defined in web.xml and jetty-env.xml. 我花了两天的时间来找到web.xml和jetty-env.xml中定义的名称之间的区别。

Take a look in : https://www.eclipse.org/jetty/documentation/9.4.x/using-jetty-jndi.html 请查看: 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. jetty.xml jetty.xml文件中定义的命名资源的范围是JVM级别或服务器级别。 The classes for the resource must be visible at the Jetty container level. 资源的类必须在Jetty容器级别可见。 If the classes for the resource only exist inside your webapp, you must declare it in a WEB-INF/jetty-env.xml file. 如果资源的类仅存在于webapp中,则必须在WEB-INF / jetty-env.xml文件中声明它。

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. WEB-INF / jetty-env.xml WEB-INF / jetty-env.xml文件中的命名资源范围限定为文件所在的Web应用程序。 While you can enter JVM or Server scopes if you choose, we do not recommend doing so. 虽然您可以选择输入JVM或服务器范围,但我们不建议您这样做。 The resources defined here may use classes from inside your webapp. 这里定义的资源可以使用您的webapp内部的类。 This is a Jetty-specific mechanism. 这是Jetty特定的机制。

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. 上下文xml文件上下文xml文件中的条目应限定在它们应用的webapp级别,但如果您选择,可以提供不太严格的服务器或JVM范围级别。 As with resources declared in a jetty.xml file, classes associated with the resource must be visible on the container's classpath. 与jetty.xml文件中声明的资源一样,与资源关联的类必须在容器的类路径上可见。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM