简体   繁体   English

尝试查找context.xml中声明的资源时,javax.naming.NameNotFoundException

[英]javax.naming.NameNotFoundException when trying to lookup resource declared in context.xml

I am deploying a WAR to JBoss EAP 7. In my WAR's META-INF/context.xml file I have the following: 我正在将WAR部署到JBoss EAP7。在我的WAR的META-INF/context.xml文件中,我具有以下内容:

<Context unloadDelay="500000">
    <Resource name="jdbc/sybase/somedb"
          auth="Container"
          type="javax.sql.DataSource"
          driverClassName="net.sourceforge.jtds.jdbc.Driver"
          url="jdbc:jtds:sybase://localhost:12501/somedb"
          username="username" password="secret"
          validationQuery="select 1"              
          maxActive="2" maxIdle="0" maxWait="-1"/>          
...

From my Java code I try to obtain the DataSource doing a: 从我的Java代码中,我尝试通过以下方式获取DataSource:

InitialContext cxt = new InitialContext(); 
DataSource ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/sybase/somedb" );

The exact above code works and the name is found in the context when I deploy to Tomcat 8 but not when I deploy to JBoss EAP 7. In the latter case I get: 上面的确切代码可以工作,并且当我部署到Tomcat 8时可以在上下文中找到该名称,而当我部署到JBoss EAP 7时却找不到。在后一种情况下,我得到:

Caused by: javax.naming.NameNotFoundException: comp/env/jdbc/sybase/somedb -- service jboss.naming.context.java.comp.env.jdbc.sybase.somedb
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:235)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)

What am I doing wrong and how can I fix the above problem? 我在做什么错,如何解决以上问题?

Your META-INF/context.xml file is a Tomcat deployment descriptor (not defined by the Java EE specification) so it is not seen or parsed by JBoss EAP 7. 您的META-INF/context.xml文件是Tomcat部署描述符(Java EE规范未定义),因此JBoss EAP 7无法看到或解析它。

There are many alternatives to this including the solution to is there a standard way to define a JDBC Datasource for Java EE containers . 有很多替代方法,包括解决方案,该方法是否存在为Java EE容器定义JDBC数据源的标准方法

If you were to ask RedHat support they would likely recommend that you create the datasource using server administration tools such as the admin console or jboss-cli.sh . 如果您需要RedHat支持,他们可能会建议您使用服务器管理工​​具(例如管理控制台或jboss-cli.sh创建数据源。 This decouples your application from the datasource definition so that you can specify environment specific settings (such as pool sizes and hostnames) without repackaging your WAR.file. 这使您的应用程序与数据源定义脱钩,以便您可以指定环境特定的设置(例如池大小和主机名),而无需重新打包WAR.file。 This method also requires you to deploy the JDBC driver jar separately from your application. 此方法还要求您与应用程序分开部署JDBC驱动程序jar。

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

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