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 ?