您应该在server.xml或context.xml中设置数据库连接属性 [英] Should you set up database connection properties in server.xml or context.xml

查看:125
本文介绍了您应该在server.xml或context.xml中设置数据库连接属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JNDI为Spring Web应用程序设置数据库连接属性。

I am trying to set up the database connection properties using JNDI for a Spring web application.

我正在考虑以下两种方法:

I am considering two approaches as below:

方法1:

在Spring配置中,您可能会遇到以下情况:

In your Spring configuration you may have something like:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/facs"/>

然后在你的webapp /META-INF/context.xml文件中你也应该有类似的东西: / p>

Then in your webapp /META-INF/context.xml file you should have something similar too:

<?xml version='1.0' encoding='utf-8'?>

<!-- antiResourceLocking="true" -->
<Context path="/podd-apn"
         reloadable="true"
         cachingAllowed="false"
         antiResourceLocking="true"
         >

  <Resource name="jdbc/facs"              
            type="javax.sql.DataSource" username="${database.username}" password="${database.password}"
            driverClassName="org.postgresql.Driver" 
            url="${database.url}"
            maxActive="8" maxIdle="4"
            global="jdbc/facs" 
            />


</Context>

在你的web.xml中你应该这样:

And in your web.xml you should something like:

<!-- JNDI -->
  <resource-ref>
    <description>FACs Datasource</description>
    <res-ref-name>jdbc/facs</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref> 



方法2:

在Spring上下文中设置如下:

Setup in the Spring context like this:

<jee:jndi-lookup id="dbDataSource"
   jndi-name="jdbc/DatabaseName"
   expected-type="javax.sql.DataSource" />

您可以使用以下内容在Tomcat的server.xml中声明JNDI资源:

You can declare the JNDI resource in Tomcat's server.xml using something like this:

<GlobalNamingResources>
  <Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
              username="dbUsername" password="dbPasswd"
              url="jdbc:postgresql://localhost/dbname"
              driverClassName="org.postgresql.Driver"
              initialSize="5" maxWait="5000"
              maxActive="120" maxIdle="5"
              validationQuery="select 1"
              poolPreparedStatements="true"/>
</GlobalNamingResources/>

并从Tomcat的web context.xml引用JNDI资源,如下所示:

And reference the JNDI resource from Tomcat's web context.xml like this:

<ResourceLink name="jdbc/DatabaseName"
   global="jdbc/DatabaseName"
   type="javax.sql.DataSource"/>



我的问题是保存数据库属性的最佳位置在哪里?它们应该放在 server.xml 还是 context.xml

另外,如果我有2个数据库,我应该使用两个配置吗?

Also, if I have 2 databases, should I use two configs?

另外,最好将它们直接放在server.xml或context.xml中吗?或者我是否需要通过Tomcat Manager GUI控制台进行配置?

Also, is it best practice to directly place them in either server.xml or context.xml? Or do I need to configure through Tomcat Manager GUI console?

谢谢!

推荐答案

我更喜欢采用第三种方法,从
方法1 方法2 user1016403描述

I prefer a third approach that takes the best from Approach 1 and Approach 2 described by user1016403.


  1. server.xml上保存数据库属性

  2. 引用 server.xml 来自Web应用程序的数据库属性 META-INF / context.xml

  1. Save database properties on the server.xml
  2. reference the server.xml database properties from the web application META-INF/context.xml



方法3的好处



虽然第一点对于安全性原因很有用,但第二点对于引用服务器属性值非常有用。 Web应用程序,即使服务器属性值将发生变化。

Approach 3 benefits

While the first point is useful for security reasons the second point is useful for referencing server properties value from the web application, even if server properties values will change.

此外,将服务器上的资源定义与Web应用程序的使用分离n使得这种配置可以在具有各种复杂性的组织中进行扩展,其中不同的团队在不同的层/层上工作:如果管理员与每个资源的开发人员共享相同的JNDI名称,则服务器管理员团队可以在不与开发人员团队冲突的情况下工作。

Moreover decoupling resource definitions on the server from their use by the web application makes such configuration scalable across organizations with various complexity where different teams work on different tiers/layers: the server administrators team can work without conflicting with developers team if the administrator shares the same JNDI name with the developer for each resource.

定义JNDI名称 jdbc / ApplicationContext_DatabaseName

在Tomcat的 server.xml中声明 jdbc / ApplicationContext_DatabaseName 的各种属性和值使用以下内容:

Declare the jdbc/ApplicationContext_DatabaseName's various properties and values in Tomcat's server.xml using something like this:

<GlobalNamingResources>
  <Resource name="jdbc/ApplicationContext_DatabaseName" auth="Container" type="javax.sql.DataSource"
              username="dbUsername" password="dbPasswd"
              url="jdbc:postgresql://localhost/dbname"
              driverClassName="org.postgresql.Driver"
              initialSize="5" maxWait="5000"
              maxActive="120" maxIdle="5"
              validationQuery="select 1"
              poolPreparedStatements="true"/>
</GlobalNamingResources/>

链接 jdbc / ApplicationContext_DatabaseName 的属性来自Web应用程序 META-INF / context.xml 由应用程序私有JNDI上下文 java:comp / env / 指定在名称属性中:

Link the jdbc/ApplicationContext_DatabaseName's properties from web application META-INF/context.xml by an application-private JNDI context java:comp/env/ specified in the name attribute:

<Context path="/ApplicationContext" ... >
  <!--
    "global" attribute links to GlobalNamingResources in the ${catalina.base}/conf/server.xml (server administrator team)
    "name" attribute is relative to the application-private JNDI context java:comp/env/ and is looked up from the java web application (application developer team)
  -->
  <ResourceLink global="jdbc/ApplicationContext_DatabaseName" name="jdbc/DatabaseName" type="javax.sql.DataSource"/>
</Context>

最后,为了使用JNDI资源,请指定JNDI名称 jdbc Web应用程序部署描述符中的/ DatabaseName

Finally, in order to use the JNDI resource, specify the JNDI name jdbc/DatabaseName in web application's deployment descriptor:

<resource-ref>
    <description>DatabaseName's Datasource</description>
    <res-ref-name>jdbc/DatabaseName</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref> 

并且在Spring上下文中:

and in Spring context:

<jee:jndi-lookup id="DatabaseNameDataSource"
   jndi-name="jdbc/DatabaseName"
   expected-type="javax.sql.DataSource" />



接近3个缺点



如果是JNDI名称被更改,然后必须编辑 server.xml META-INF / context.xml 部署是必要的;然而,这种情况很少见。

Approach 3 drawbacks

If the JNDI name gets changed then both the server.xml and the META-INF/context.xml will have to be edited and a deploy would be necessary; nevertheless this scenario is rare.

一个Web应用程序使用的许多数据源

只需将配置添加到Tomcat的 server.xml

Simply add configurations to Tomcat's server.xml:

<GlobalNamingResources>
  <Resource name="jdbc/ApplicationContext_DatabaseName1" ... />
  <Resource name="jdbc/ApplicationContext_DatabaseName2" ... />
  ...
</GlobalNamingResources/>

添加链接Web应用程序 META-INF / context.xml 由应用程序私有JNDI上下文 java:comp / env / name 属性中指定:

Add link web application META-INF/context.xml by an application-private JNDI context java:comp/env/ specified in the name attribute:

<Context path="/ApplicationContext" ... >
  <ResourceLink global="jdbc/ApplicationContext_DatabaseName1" name="jdbc/DatabaseName1" ... />
  <ResourceLink global="jdbc/ApplicationContext_DatabaseName2" name="jdbc/DatabaseName2" ... />
  ...
</Context>

最后在Web应用程序的部署描述符中添加JNDI资源使用情况:

Finally add JNDI resources usage in web application's deployment descriptor:

<resource-ref>
    <description>DatabaseName1's Datasource</description>
    <res-ref-name>jdbc/DatabaseName1</res-ref-name> ... 
</resource-ref> 
<resource-ref>
    <description>DatabaseName2's Datasource</description>
    <res-ref-name>jdbc/DatabaseName2</res-ref-name> ... 
</resource-ref>
...

并且在Spring上下文中:

and in Spring context:

<jee:jndi-lookup id="DatabaseName1DataSource"
   jndi-name="jdbc/DatabaseName1" ... />
<jee:jndi-lookup id="DatabaseName2DataSource"
   jndi-name="jdbc/DatabaseName2" ... />
...



许多数据来源许多Web应用程序在同一台服务器上使用

只需将配置添加到Tomcat的 server.xml

Simply add configuration to Tomcat's server.xml:

<GlobalNamingResources>
  <Resource name="jdbc/ApplicationContextX_DatabaseName1" ... />
  <Resource name="jdbc/ApplicationContextX_DatabaseName2" ... />
  <Resource name="jdbc/ApplicationContextY_DatabaseName1" ... />
  <Resource name="jdbc/ApplicationContextY_DatabaseName2" ... />
  ...
</GlobalNamingResources/>

其他配置应该可以从以前的变体案例中扣除。

the others configuration should be deducible from previous variation case.



同一服务器上许多Web应用程序使用的同一数据库的许多数据源

在这种情况下,Tomcat的 server.xml 配置如:

In such case a Tomcat's server.xml configurations like:

<GlobalNamingResources>
  <Resource name="jdbc/ApplicationContextX_DatabaseName" ... />
  <Resource name="jdbc/ApplicationContextY_DatabaseName" ... />

在两个不同的Web应用程序中结束 META-INF / context.xml like:

ends up in two different web applications META-INF/context.xml like:

<Context path="/ApplicationContextX" ... >
  <ResourceLink global="jdbc/ApplicationContextX_DatabaseName" name="jdbc/DatabaseName" ... />
</Context>

并且喜欢:

<Context path="/ApplicationContextY" ... >
  <ResourceLink global="jdbc/ApplicationContextY_DatabaseName" name="jdbc/DatabaseName" ... />
</Context>

所以有人可能会担心同样的 name =jdbc查找/ DatabaseName,然后由同一服务器上部署的两个不同应用程序使用:这不是问题,因为 jdbc / DatabaseName 是一个应用程序私有JNDI上下文 java:comp / env / ,所以 ApplicationContextX 使用 java:comp / env / 无法(按设计)查找链接到 global =jdbc / ApplicationContextY_DatabaseName的资源

so someone might be worried about the fact that the same name="jdbc/DatabaseName" is looked up, and then used, by two different applications deployed on the same server: this is not a problem because the jdbc/DatabaseName is an application-private JNDI context java:comp/env/, so ApplicationContextX by using java:comp/env/ can't (by design) look up the resource linked to global="jdbc/ApplicationContextY_DatabaseName".

当然如果您在没有这种担心的情况下感到更放松,您可以使用不同的命名策略,例如:

Of course if you felt more relaxed without this worry you might use a different naming strategy like:

<Context path="/ApplicationContextX" ... >
  <ResourceLink global="jdbc/ApplicationContextX_DatabaseName" name="jdbc/applicationXprivateDatabaseName" ... />
</Context>

并且喜欢:

<Context path="/ApplicationContextY" ... >
  <ResourceLink global="jdbc/ApplicationContextY_DatabaseName" name="jdbc/applicationYprivateDatabaseName" ... />
</Context>

这篇关于您应该在server.xml或context.xml中设置数据库连接属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆