title: Adding JNDI to embedded Tomcat server in Grails 3
body: <p>When running test-app in grails 3.0, or run-app, grails runs its own version of the embedded Tomcat server. I was able to conclude this from the following link: <a href="https://roshandawrani.wordpress.com/2011/03/13/grails-tip-configuring-embedded-tomcat-instance-used-in-developmenttest-env/" rel="nofollow">https://roshandawrani.wordpress.com/2011/03/13/grails-tip-configuring-embedded-tomcat-instance-used-in-developmenttest-env/</a></p>
  
  <p>However, the context.xml and server.xml files are precompiled with the pulled down libraries. When creating a  grails app from scratch, I cannot find either of there two files. Same is true for config.groovy, as it is located within an external library. </p>
  
  <p>I am trying to inject JNDI resources, into the container, so that I can invoke them. Something like this:</p>
  
  <pre><code>&lt;Resource name="myDatasourceName" auth="Container" type="javax.sql.DataSource"
      maxActive="100" maxIdle="30" maxWait="10000"
      username="root" password="password" driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://localhost:3306/my_db_name"/&gt;
  </code></pre>
  
  <p>In the first link, the authors provide a way to do it in a scripts/_Events.groovy directory, but I do not have this either.</p>
  
  <p>UPDATE 1: Non-working code</p>
  
  <pre><code>import grails.boot.GrailsApp
  import grails.boot.config.GrailsAutoConfiguration
  import org.apache.catalina.Context
  import org.apache.catalina.startup.Tomcat
  import org.apache.tomcat.util.descriptor.web.ContextResource
  import org.springframework.boot.autoconfigure.SpringBootApplication
  import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer
  import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
  import org.springframework.context.annotation.Bean
  
  @SpringBootApplication
  class Application extends GrailsAutoConfiguration {
      static void main(String[] args) {
          GrailsApp.run(Application, args)
      }
  
      @Bean
      public TomcatEmbeddedServletContainerFactory tomcatFactory() {
          return new TomcatEmbeddedServletContainerFactory() {
  
              @Override
              protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                      Tomcat tomcat) {
                  tomcat.enableNaming();
                  return super.getTomcatEmbeddedServletContainer(tomcat);
              }
  
              @Override
              protected void postProcessContext(Context context) {
                  context.getNamingResources().addResource(preconfigureDbResource("oneSource", "127.0.0.1"))
                  context.getNamingResources().addResource(preconfigureDbResource("nextSource", "127.0.0.1"))
              }
          }
  
      }
  
      private ContextResource preconfigureDbResource(String name, String ip) {
          ContextResource resource = new ContextResource()
          resource.setType("javax.sql.DataSource")
          resource.setName("jdbc/" + name)
          resource.setProperty("url", "jdbc:oracle:thin:@" + ip + ":1521:ucop")
          resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver")
          resource.setProperty("username", "coolio")
          resource.setProperty("password", "password")
          resource.setProperty("auth", "Container")
          resource.setProperty("maxTotal", "100")
          resource.setProperty("maxIdle", "30")
          resource.setProperty("maxWaitMillis", "10000")
          return resource;
      }
  
  
  }
  </code></pre>
  
  <p>I am calling this source like this in my service file:</p>
  
  <pre><code>public DataSource getOneSource() {
      Context context = (Context) new InitialContext().lookup("java:/comp/env")
          oneSource= (DataSource) context.lookup("jdbc/oneSource")
      return oneSource
  }    
  </code></pre>
  
  <p>But I am getting an error stating:</p>
  
  <pre><code>javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
  </code></pre>
  
  <p>Has anyone done this before? I would not be surprised if there is an extra thread that is overwriting the context.</p>