title: Grails JNDI data source not working
body: <p>I have a grails application with a data source defined in DataSource.groovy as:</p>
  
  <pre><code>dataSource {
      pooled = true
      driverClassName = "oracle.jdbc.OracleDriver"
      username = "myusername"
      password = "mypassword"
  }
  hibernate {
      cache.use_second_level_cache = true
      cache.use_query_cache = false
      cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
  }
  // environment specific settings
  environments {
      development {
          dataSource {
              dbCreate = "validate"
              url = "jdbc:oracle:thin:@server:1521:instance"
          logSql = true
          }
  
      }
  }
  </code></pre>
  
  <p>This all works fine but I want to use a JNDI data source.</p>
  
  <p>In Config.groovy I have:</p>
  
  <pre><code>grails.naming.entries = [
  'jdbc/pms_dev': [
      type: 'java.sql.DataSource',
      auth: 'Container',
      description: 'Main datasource',
  
      url: 'jdbc:oracle:thin:@server:1521:instance',
      username: "myusername",
      password: "mypassword",
      driverClassName: "oracle.jdbc.OracleDriver",
      maxActive: "8",
      maxIdle: "4"
      ]
  ]
  </code></pre>
  
  <p>I change DataSource.groovy's development section to:</p>
  
  <pre><code>development {
      dataSource {
          dbCreate = "validate"
          jndiName = "java:comp/env/jdbc/pms_dev"
          logSql = true
      }
  }
  </code></pre>
  
  <p>Now I get the following error when do grails runApp:</p>
  
  <pre><code>| Error 2014-02-06 11:25:58,402 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
     Line | Method
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by NamingException: Cannot create resource instance
  -&gt;&gt; 146 | getObjectInstance in org.apache.naming.factory.ResourceFactory
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   304 | getObjectInstance in javax.naming.spi.NamingManager
  |   843 | lookup . in org.apache.naming.NamingContext
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   168 | lookup   in     ''
  |   158 | lookup . in org.apache.naming.SelectorContext
  |   392 | lookup   in javax.naming.InitialContext
  |   303 | innerRun in java.util.concurrent.FutureTask$Sync
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  | Error 2014-02-06 11:25:58,574 [localhost-startStop-1] ERROR context.GrailsContextLoader  - Error initializing Grails: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
     Line | Method
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by NamingException: Cannot create resource instance
  -&gt;&gt; 146 | getObjectInstance in org.apache.naming.factory.ResourceFactory
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   304 | getObjectInstance in javax.naming.spi.NamingManager
  |   843 | lookup . in org.apache.naming.NamingContext
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   168 | lookup   in     ''
  |   158 | lookup . in org.apache.naming.SelectorContext
  |   392 | lookup   in javax.naming.InitialContext
  |   303 | innerRun in java.util.concurrent.FutureTask$Sync
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  | Error 2014-02-06 11:25:58,887 [localhost-startStop-1] ERROR [localhost].[/osrpms]  - Exception sending context initialized event to listener instance of class org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener
  Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
     Line | Method
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'dataSource': Cannot resolve reference to bean 'dataSourceUnproxied' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by BeanCreationException: Error creating bean with name 'dataSourceUnproxied': Invocation of init method failed; nested exception is javax.naming.NamingException: Cannot create resource instance
  -&gt;&gt; 303 | innerRun in java.util.concurrent.FutureTask$Sync
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  Caused by NamingException: Cannot create resource instance
  -&gt;&gt; 146 | getObjectInstance in org.apache.naming.factory.ResourceFactory
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   304 | getObjectInstance in javax.naming.spi.NamingManager
  |   843 | lookup . in org.apache.naming.NamingContext
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   154 | lookup   in     ''
  |   831 | lookup . in     ''
  |   168 | lookup   in     ''
  |   158 | lookup . in org.apache.naming.SelectorContext
  |   392 | lookup   in javax.naming.InitialContext
  |   303 | innerRun in java.util.concurrent.FutureTask$Sync
  |   138 | run      in java.util.concurrent.FutureTask
  |   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
  |   908 | run      in     ''
  ^   662 | run . .  in java.lang.Thread
  | Error 2014-02-06 11:25:59,184 [localhost-startStop-1] ERROR core.StandardContext  - Error listenerStart
  | Error 2014-02-06 11:25:59,184 [localhost-startStop-1] ERROR core.StandardContext  - Context [/osrpms] startup failed due to previous errors
  </code></pre>