英文:
JNDI Resources in web-fragment
问题
我正在制作一个网络片段,以隔离只在整个生产流程的某一部分中需要的专门过程。我试图定义一个特定于网络片段的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.xml,其中包含与主应用程序无关的项。
我尝试将资源链接从主context.xml移动到网络片段的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.xml甚至web-fragment的context.xml。两种情况都返回相同的错误。
虽然我可以接受这一个实例,但我知道我们将会添加和/或迁移更多的部分到网络片段,并且我真的希望将资源引用分离到它们各自的jar中。
编辑
我应该提到,我是在Eclipse内运行Tomcat时测试所有这些。我不认为这会有什么区别,但我想提一下
这种操作是否可能?
英文:
I'm working on a web-fragment to isolate a specialized process needed in only one part of our overall production process. I'm trying to define a JNDI resource specific to the web-fragment, but I keep running into a brick wall.
The application is running in Tomcat 9.
I am using Web-Fragment specification 4.0.
The working model, right now, is I have the database connection defined as a global resource in the server.xml as such:
<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}" />
and is referenced in the larger web application 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>
This works as expected and I can connect to the resource and retrieve data. However, I do not want to pollute the main application's context.cml with items that do not pertain to the main application.
I've tried moving the resource link from the main context.xml to the web-fragment's context.xml but that results in the following error:
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)
I've moving the resource definition from the server.xml to the web-fragment's web-fragment.xml and even the web-fragment's context.xml. Both instances return the same error.
While I can live with this one instance, I know we will be adding and/or migrating more pieces to web-fragments and I really want to separate the resource references to their specific jar.
> EDIT
I should mention I am testing all of this while running tomcat from inside Eclipse. I don't think that will make a difference, but I want to mention it
Is this even possible?
答案1
得分: 0
我们已经决定将应用程序容器化。因此,由于我们在生产过程中有多个位置,这将不再是一个问题。
通过将资源链接 <ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" />
放置在各个位置的 [TOMCAT_HOME]/conf/context.xml
中,我们消除了在主应用程序的 /META-INF/context.xml
中添加杂项的需求。
这应该可以正常工作,因为我们将每个位置的要求分为单独的 web-fragment 项目。
英文:
We have decided to containerize the application. Thus, as we have multiple locations in our production process, this will become a non-issue.
By placing the resource link <ResourceLink global="localappserver" name="localappserver" type="javax.sql.DataSource" />
in the individual location's [TOMCAT_HOME]/conf/context.xml
we eliminate the need to clutter the main application's /META-INF/context.xml
This should work as we divide up each location's requirements into individual web-fragment projects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论