Spring Boot Using Embedded Tomcat with JNDI
Asked Answered
I

2

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)
Irvine answered 24/1, 2015 at 23:42 Comment(1)
Already answered here: #24942329Kaon
B
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/.

Barbey answered 25/1, 2015 at 7:35 Comment(4)
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?Irvine
You're using the wrong JNDI name when performing the look up. See my edit for more details.Barbey
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 errorIrvine
FYI, the workaround/hack for lookup during startup is fixed in Spring Boot 1.4Tiphani
W
-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
Wallpaper answered 19/7, 2017 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.