0

I did git clone on the Spring Boot Sample JNDI app on Github, ran it in embedded mode at it works OK.

So, now I want to convert it to run on a separate Tomcat instance.

Firstly, I changed the pom.xml packaging to war.

Secondly, I changed SampleTomcatJndiApplication class to extend SpringBootServletInitializer add added the following method:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SampleTomcatJndiApplication.class);
} 

I also changed the JNDI properties. The Tomcat instance (Tomcat 8.0.24) has the following context defined in server.xml:

    <Resource auth="Container" 
    driverClassName="com.mysql.jdbc.Driver" 
    maxActive="8" maxIdle="4" 
    name="jdbc/finance" type="javax.sql.DataSource" 
    url="jdbc:mysql://localhost:3306/finance" 
    username="xxxx" password="yyyy" />

My DataSource bean is defined as

@Bean(destroyMethod="")
public DataSource jndiDataSource() throws IllegalArgumentException, NamingException {
    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
    bean.setJndiName("java:comp/env/jdbc/finance");
    bean.setProxyInterface(DataSource.class);
    bean.setLookupOnStartup(false);
    bean.afterPropertiesSet();
    return (DataSource)bean.getObject();
}

When running on an existing Tomcat instance, I always get this exception:

Name [jdbc/finance] is not bound in this Context. Unable to find [jdbc].

When running in embedded mode, it works OK.

How can I get this to work on an existing Tomcat ?

NickJ
  • 9,380
  • 9
  • 51
  • 74

1 Answers1

0

The problem was not in my application at all, but in my Tomcat configuration. Thanks to the accepted answer to this question, I found out that I needed to add a ResourceLink element to Tomcat's context.xml. I wonder why the Tomcat JNDI HowTo makes no mention of it?

Community
  • 1
  • 1
NickJ
  • 9,380
  • 9
  • 51
  • 74