java 在jetty中定义两个数据源(jetty-env.xml)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11329022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Defining two data sources in jetty (jetty-env.xml)
提问by polypiel
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:
我正在尝试使用该jetty-env.xml
文件在我的 Web 应用程序中定义两个数据源。它仅适用于一个数据源,但在添加第二个数据源时出现此异常:
java.lang.IllegalStateException: Nothing to bind for name javax.sql.DataSource/default
Here's my configuration:
这是我的配置:
jetty-env.xml
码头环境.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
网页.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?
有什么线索吗?
回答by Tim
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”是范围,没有它,您的其余论点就不合适,并且可能会导致您的问题。
回答by janih
The id parameter values should match in jetty-env.xml and web.xml
id 参数值应该在 jetty-env.xml 和 web.xml 中匹配
jetty-env.xml
码头环境.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
网页.xml
<resource-ref id="DS1">...</resource-ref>
<resource-ref id="DS2">...</resource-ref>
回答by javierdotnet
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.xmlNaming 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.
jetty.xml在 jetty.xml 文件中定义的命名资源的范围在 JVM 级别或服务器级别。资源的类必须在 Jetty 容器级别可见。如果资源的类只存在于您的 web 应用程序中,您必须在 WEB-INF/jetty-env.xml 文件中声明它。
WEB-INF/jetty-env.xmlNaming 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.
WEB-INF/jetty-env.xmlWEB-INF/jetty-env.xml 文件中的命名资源范围限定为该文件所在的 Web 应用程序。虽然您可以选择输入 JVM 或服务器范围,但我们不建议这样做。此处定义的资源可能会使用您的 web 应用程序内部的类。这是 Jetty 特有的机制。
Context xml fileEntries 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.
上下文 xml 文件 上下文 xml 文件中的条目的范围应在它们所应用的 Web 应用程序级别,尽管您可以选择提供较不严格的服务器或 JVM 范围级别。与在 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>
回答by vlk32
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.
尝试在 Jetty 中启用日志记录。注意记录器名称是“jndi”。Jetty 开发人员不使用类名作为 JNDI 的记录器名称。
I spent 2 days to finding difference between name defined in web.xml and jetty-env.xml.
我花了 2 天时间找到 web.xml 和 jetty-env.xml 中定义的名称之间的差异。