title: Should you set up database connection properties in server.xml or context.xml
body: <p>I am trying to set up the database connection properties using JNDI for a Spring web application.</p>
  
  <p>I am considering two approaches as below:</p>
  
  <p><strong>Approach 1:</strong></p>
  
  <p>In your Spring configuration you may have something like:</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/facs"/&gt;
  </code></pre>
  
  <p>Then in your webapp /META-INF/context.xml file you should have something similar too:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  &lt;!-- antiResourceLocking="true" --&gt;
  &lt;Context path="/podd-apn"
           reloadable="true"
           cachingAllowed="false"
           antiResourceLocking="true"
           &gt;
  
    &lt;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" 
              /&gt;
  
  
  &lt;/Context&gt;
  </code></pre>
  
  <p>And in your web.xml you should something like:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;!-- JNDI --&gt;
    &lt;resource-ref&gt;
      &lt;description&gt;FACs Datasource&lt;/description&gt;
      &lt;res-ref-name&gt;jdbc/facs&lt;/res-ref-name&gt;
      &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
      &lt;res-auth&gt;Container&lt;/res-auth&gt;
    &lt;/resource-ref&gt; 
  </code></pre>
  
  <p><br>
    <strong>Approach 2:</strong></p>
  
  <p>Setup in the Spring context like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;jee:jndi-lookup id="dbDataSource"
     jndi-name="jdbc/DatabaseName"
     expected-type="javax.sql.DataSource" /&gt;
  </code></pre>
  
  <p>You can declare the JNDI resource in Tomcat's server.xml using something like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;GlobalNamingResources&gt;
    &lt;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"/&gt;
  &lt;/GlobalNamingResources/&gt;
  </code></pre>
  
  <p>And reference the JNDI resource from Tomcat's web context.xml like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ResourceLink name="jdbc/DatabaseName"
     global="jdbc/DatabaseName"
     type="javax.sql.DataSource"/&gt;
  </code></pre>
  
  <p><br>
  My question is where is the best place to keep database properties? Should they be placed in <strong>server.xml</strong> or <strong>context.xml</strong>?</p>
  
  <p>Also, if I have 2 databases, should I use two  configs? </p>
  
  <p>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?</p>
  
  <p>Thanks!</p>