title: Spring JNDIDataSourceLookup failed to lookup for JNDSI datasource in Glassfish?
body: <p>I get an error whenever I try to deploy my Spring war application to Glassfish server ( with Glassfish 4 and 5 ):</p>
  <p>However, everything is okay if I run the application straight from the Eclipse. My project is Maven project, I just right click the projeck and the choose Run As -&gt; Run On server</p>
  <p>Here's my glassfish-resources.xml, its located in the WEB-INF folder and its included in the deployment ( I've verified it )</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;!DOCTYPE resources PUBLIC &quot;-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN&quot; &quot;http://glassfish.org/dtds/glassfish-resources_1_5.dtd&quot;&gt;
  &lt;resources&gt;
      &lt;jdbc-connection-pool allow-non-component-callers=&quot;false&quot; associate-with-thread=&quot;false&quot; connection-creation-retry-attempts=&quot;0&quot; connection-creation-retry-interval-in-seconds=&quot;10&quot; connection-leak-reclaim=&quot;false&quot; connection-leak-timeout-in-seconds=&quot;0&quot; connection-validation-method=&quot;auto-commit&quot; datasource-classname=&quot;com.mysql.jdbc.jdbc2.optional.MysqlDataSource&quot; fail-all-connections=&quot;false&quot; idle-timeout-in-seconds=&quot;300&quot; is-connection-validation-required=&quot;false&quot; is-isolation-level-guaranteed=&quot;true&quot; lazy-connection-association=&quot;false&quot; lazy-connection-enlistment=&quot;false&quot; match-connections=&quot;false&quot; max-connection-usage-count=&quot;0&quot; max-pool-size=&quot;32&quot; max-wait-time-in-millis=&quot;60000&quot; name=&quot;mysql_pos_rootPool&quot; non-transactional-connections=&quot;false&quot; pool-resize-quantity=&quot;2&quot; res-type=&quot;javax.sql.DataSource&quot; statement-timeout-in-seconds=&quot;-1&quot; steady-pool-size=&quot;8&quot; validate-atmost-once-period-in-seconds=&quot;0&quot; wrap-jdbc-objects=&quot;false&quot;&gt;
          &lt;property name=&quot;serverName&quot; value=&quot;localhost&quot;/&gt;
          &lt;property name=&quot;portNumber&quot; value=&quot;3306&quot;/&gt;
          &lt;property name=&quot;databaseName&quot; value=&quot;pos&quot;/&gt;
          &lt;property name=&quot;User&quot; value=&quot;root&quot;/&gt;
          &lt;property name=&quot;Password&quot; value=&quot;root&quot;/&gt;
          &lt;property name=&quot;URL&quot; value=&quot;jdbc:mysql://localhost:3306/pos?zeroDateTimeBehavior=convertToNull&quot;/&gt;
          &lt;property name=&quot;driverClass&quot; value=&quot;com.mysql.jdbc.Driver&quot;/&gt;
      &lt;/jdbc-connection-pool&gt;
      &lt;jdbc-resource enabled=&quot;true&quot; jndi-name=&quot;java:app/pos&quot; object-type=&quot;user&quot; pool-name=&quot;mysql_pos_rootPool&quot;/&gt;
  &lt;/resources&gt;
  </code></pre>
  <p>The beans in my RootContextConfiguration class:</p>
  <pre><code>@Bean
  public DataSource springJpaDataSource()
  {
      JndiDataSourceLookup lookup = new JndiDataSourceLookup();
      return lookup.getDataSource(&quot;java:app/pos&quot;);
  }
  
  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
  {
      Map&lt;String, Object&gt; properties = new Hashtable&lt;&gt;();
      properties.put(&quot;javax.persistence.schema-generation.database.action&quot;,
              &quot;none&quot;);
  
      HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
      adapter.setDatabasePlatform(&quot;org.hibernate.dialect.MySQL5InnoDBDialect&quot;);
  
      LocalContainerEntityManagerFactoryBean factory =
              new LocalContainerEntityManagerFactoryBean();
      factory.setJpaVendorAdapter(adapter);
      factory.setDataSource(this.springJpaDataSource());
      factory.setPackagesToScan(&quot;my.package.entity&quot;);
      factory.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
      factory.setValidationMode(ValidationMode.NONE);
      factory.setJpaPropertyMap(properties);
      return factory;
  }
  </code></pre>
  <p>The error is as below:</p>
  <blockquote>
  <p>&quot;Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in class sunwell.pos.service.config.RootContextConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean sunwell.pos.service.config.RootContextConfiguration.entityManagerFactoryBean()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springJpaDataSource' defined in class sunwell.pos.service.config.RootContextConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource sunwell.pos.service.config.RootContextConfiguration.springJpaDataSource()] threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:app/pos'; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.sun.enterprise.naming.impl.SerialContext] available at JNDI location [java:app/pos] is not assignable to [javax.sql.DataSource]. Please see server.log for more details.&quot;</p>
  </blockquote>
  <p>EDIT:</p>
  <p>I have found that during deployment, the jndi name must be prefixed with <code>jdbc</code> or something after the namespace, example: <code>java:app/jdbc/pos</code> and not just <code>java:app/pos</code>, however I'm not sure why I can run it with eclipse even without the <code>jdbc</code> prefix and in JEE application I also can reference the jndi name in persistence like this <code>&lt;jta-data-source&gt;java:app/pos&lt;/jta-data-source&gt;</code>.</p>
  <p>So bottom line is this problem only occurs during Spring war deployment. And the solution is you need to prefix the jndi name after the namespace with <code>jdbc</code> or something.</p>