title: Error trapping for a missing data source in a Spring MVC / Spring JDBC web app
body: <p>I have written a web app that uses Spring MVC libraries and Spring JDBC to connect to an Oracle DB. (I don't use any ORM type libraries as I create stored procedures on Oracle that do my stuff and I'm quite happy with that.)
  I use a connection pool to Oracle managed by the Tomcat container</p>
  
  <p>The app generally works absolutely fine by the way!</p>
  
  <p>BUT...
  I noticed the other day when I tried to set up the app on another Tomcat instance that I had forgotten to configure the connection pool and obviously the app could not get hold of an org.apache.commons.dbcp.BasicDataSource object, so it crashed.</p>
  
  <p>I define the pool params in the tomcat "<strong>context.conf</strong>"</p>
  
  <p>In my "<strong>web.xml</strong>" I have:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/Spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;!-- Map *everything* to appServlet --&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;resource-ref&gt;
      &lt;description&gt;Oracle Datasource example&lt;/description&gt;
      &lt;res-ref-name&gt;jdbc/ora1&lt;/res-ref-name&gt;
      &lt;res-type&gt;org.apache.commons.dbcp.BasicDataSource&lt;/res-type&gt;
      &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>And I have a Spring "<strong>servlet-context.xml</strong>" where JNDI is used to map the data source object provided by the connection pool to a Spring bean with the ID of "dataSource":</p>
  
  <pre><code>&lt;jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/ora1"
      resource-ref="true" /&gt;
  </code></pre>
  
  <p><strong>Here's the question: Where do I trap the case where the database cannot be accessed for whatever reason?</strong></p>
  
  <p>I don't want the user to see a yard-and-a-half of Java stack trace in their browser, rather a nicer message that tells them there is a database problem etc.
  It seems that my app tries to configure the "dataSource" bean (in "servlet-context.xml") before any code has tested it can actually provide a dataSource object from the pool?!</p>
  
  <p>Maybe I'm not fully understanding exactly what is going on in these stages of the app firing up ...</p>
  
  <p>Thanks for any advice!</p>
  
  <p><strong>UPDATE: FIXED! Let Spring configure the MVC stuff, but get a data source via the Controller</strong></p>
  
  <p>I took the <code>jndi-lookup</code> line out of the <code>servlet-context.xml</code> file, and added another class, below.
  I called the getJndiDataSource method from my Controller when I wanted to connect to the database, and needed the data source, trapping all errors in the obtaining and using of the datasource object.</p>
  
  <pre><code>import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  
  import javax.sql.DataSource;
  
  public class DataSrcConfig {
  
     public static DataSource getJndiDataSource() throws NamingException{
  
         Context initialContext = new InitialContext();
         DataSource ds = (DataSource)initialContext.lookup("java:comp/env/jdbc/ora1");
  
         return ds;
     }
  }
  </code></pre>