【问题标题】:JNDI Resources in web-fragment网络片段中的 JNDI 资源
【发布时间】:2020-11-30 07:26:09
【问题描述】:

我正在开发一个网络片段,以隔离我们整个生产流程的一部分所需的专门流程。我正在尝试定义特定于 Web 片段的 JNDI 资源,但我一直遇到障碍。

应用程序正在 Tomcat 9 中运行。 我正在使用 Web-Fragment 规范 4.0。

目前的工作模型是将数据库连接定义为 server.xml 中的全局资源,如下所示:

        <Resource name="localappserver"
                auth="Container"
                driverClassName="oracle.jdbc.OracleDriver" 
                factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory" 
                initialSize="5" 
                logAbandoned="true" 
                minEvictableIdleTimeMillis="30000" 
                maxTotal="15" 
                maxIdle="3" 
                maxWaitMillis="1000" 
                removeAbandonedOnBorrow="true" 
                removeAbandonedOnMaintenance="true" 
                removeAbandonedTimeout="15" 
                testOnBorrow="true" 
                testWhileIdle="true" 
                timeBetweenEvictionRunsMillis="300000" 
                type="javax.sql.DataSource"  
                username="${jdbc.user}"
                password="${jdbc.password}"
                url="${jdbc.url}" />

并在更大的 Web 应用程序 context.xml 中被引用

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="..." path="/..." reloadable="true" swallowOutput="true">
    
    ...
    <ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" />
    ...
    
</Context>

这按预期工作,我可以连接到资源并检索数据。但是,我不想用不属于主应用程序的项目污染主应用程序的 context.cml。

我尝试将资源链接从主 context.xml 移动到 web-fragment 的 context.xml,但这会导致以下错误:

INFO: javax.naming.NameNotFoundException: Name [localappserver] is not bound in this Context. Unable to find [localappserver].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:833)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:174)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
    at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
    at com.obj.Data.Connect(Data.java:141)

我已经将资源定义从 server.xml 移动到 web-fragment 的 web-fragment.xml 甚至 web-fragment 的 context.xml。两个实例都返回相同的错误。

虽然我可以接受这个实例,但我知道我们将添加和/或迁移更多片段到网络片段,我真的想将资源引用分离到它们的特定 jar。

编辑 我应该提到我正在测试所有这些,同时从 Eclipse 内部运行 tomcat。我认为这不会有什么不同,但我想提一下

这可能吗?

【问题讨论】:

    标签: java tomcat web-fragment


    【解决方案1】:

    我们决定将应用程序容器化。因此,由于我们在生产过程中有多个地点,这将不再是问题。

    通过将资源链接 &lt;ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" /&gt; 放置在各个位置的 [TOMCAT_HOME]/conf/context.xml 中,我们消除了将主应用程序的 /META-INF/context.xml 弄乱的需要

    当我们将每个位置的需求划分为单独的网络片段项目时,这应该会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多