title: Spring Boot using standalone Tomcat with JNDI Oracle Data Source
body: <p>I have a new Spring Boot(1.2.1.RELEASE) web/Jpa application that I want to connect to a JNDI data source (a Oracle database defined in standalone Tomcat's context.xml).</p>
  
  <p>However when I attempt this, I always get the following exception;</p>
  
  <pre><code>Unable to register MBean [org.apache.tomcat.dbcp.dbcp2.BasicDataSource@102f8ea9] with key 'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException: Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/npacore"
  </code></pre>
  
  <p>My pom.xml look like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;bootwar&lt;/artifactId&gt;
      &lt;version&gt;0.1.0&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.2.1.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
                      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;artifactId&gt;tomcat-jdbc&lt;/artifactId&gt;
                      &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;!-- &lt;dependency&gt; --&gt;
          &lt;!-- &lt;groupId&gt;oracle.ojdbc&lt;/groupId&gt; --&gt;
          &lt;!-- &lt;artifactId&gt;ojdbc&lt;/artifactId&gt; --&gt;
          &lt;!-- &lt;version&gt;6&lt;/version&gt; --&gt;
  
          &lt;!-- &lt;/dependency&gt; --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
  
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;start-class&gt;hello.Application&lt;/start-class&gt;
          &lt;java.version&gt;1.7&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;finalName&gt;${artifactId}&lt;/finalName&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;name&gt;Spring Releases&lt;/name&gt;
              &lt;url&gt;http://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
  
      &lt;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;name&gt;Spring Releases&lt;/name&gt;
              &lt;url&gt;http://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/pluginRepository&gt;
      &lt;/pluginRepositories&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>JNDI configuration looks like this in appliation.properties:</p>
  
  <pre><code>spring.datasource.jndi-name=java:/comp/env/jdbc/npacore
  spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
  spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
  </code></pre>
  
  <p>I followed very similar blog here for MySQL: <a href="https://stackoverflow.com/questions/26504913/spring-boot-with-jndi-data-source">Spring Boot with JNDI Datasource</a></p>