3

I am using Spring Boot with Embedded Tomcat and attempting to use JNDI but getting the following error:

javax.naming.NameNotFoundException: Name [jdbc/dataSource]

Any tips would be greatly appreciated.

Here is my code:

@Configuration
public class TomcatJndiConfiguration{

@Value("${database.driver}")
private String driverClassName;

@Value("${database.url}")
private String databaseUrl;

@Value("${database.username}")
private String databaseUsername;

@Value("${database.password}")
private String databasePassword;

@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) {
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/dataSource");
            resource.setType(DataSource.class.getName());
            resource.setProperty("driverClassName", driverClassName);
            resource.setProperty("url", databaseUrl);
            resource.setProperty("password", databaseUsername);
            resource.setProperty("username", databasePassword);
            context.getNamingResources().addResource(resource);
        }
    };
}

@Bean
public DataSource dataSource() throws IllegalArgumentException, NamingException {
    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
    bean.setJndiName("jdbc/dataSource");
    bean.setLookupOnStartup(true);
    bean.setProxyInterface(DataSource.class);
    bean.setResourceRef(true);
    bean.afterPropertiesSet();
    return (DataSource) bean.getObject();

}
Stacktrace is: 
Caused by: javax.naming.NameNotFoundException: Name [jdbc/dataSource] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
at org.springframework.jndi.JndiObjectTargetSource.afterPropertiesSet(JndiObjectTargetSource.java:97)
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.createJndiObjectProxy(JndiObjectFactoryBean.java:318)
at org.springframework.jndi.JndiObjectFactoryBean$JndiObjectProxyFactory.access$000(JndiObjectFactoryBean.java:307)
at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:200)
at com.kronos.daas.configuration.TomcatJndiConfiguration.dataSource(TomcatJndiConfiguration.java:72)
shep519
  • 56
  • 1
  • 1
  • 4
  • Already answered here: http://stackoverflow.com/questions/24941829/how-to-create-jndi-context-in-spring-boot-with-embedded-tomcat-container/26005740#26005740 – nekperu15739 Sep 29 '16 at 16:00

2 Answers2

2

You need to set lookupOnStartup to false on the JndiObjectFactoryBean.

Alternatively, if you need the lookup to work during startup, then this answer may be of interest.

Edit: you've also set the JNDI name on your JndiObjectFactory bean incorrectly. It needs to be java:comp/env/jdbc/myDataSource not jdbc/dataSource.

You use a different name when you're looking up the resource versus when you registered it as the registration automatically places the resource beneath java:comp/env/.

Community
  • 1
  • 1
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • hmm still no luck. Had to modify a few lines since some methods are deprecated. Get classLoader: ClassUtils.overrideThreadContextClassLoader(context.getClass().getClassLoader()); and I still receive the same error as above. Any ideas? – shep519 Jan 25 '15 at 15:32
  • You're using the wrong JNDI name when performing the look up. See my edit for more details. – Andy Wilkinson Jan 26 '15 at 11:30
  • I have tried that as well, but I don't believe that matters if I set setResourceRef to true it should append the "java:" string automatically, but either way I still receive the same error – shep519 Jan 26 '15 at 18:12
  • FYI, the workaround/hack for lookup during startup is fixed in Spring Boot 1.4 – dustin.schultz Apr 21 '16 at 02:52
-1

If you are using spring boot, no need for all of that class. It is already configured in @EnableAutoConfiguration or @SpringBootApplication

Just put the following in your application.properties file or equivalent in application.yml file

spring.datasource.driverClassName=JDBCDriverClassName
spring.datasource.url=databaseUrl
spring.datasource.username=databaseUsername
spring.datasource.password=databasePassword
spring.datasource.jndi-name=java:jdbc/dataSource
Hany Sakr
  • 2,591
  • 28
  • 27