How do you use a Tomcat JNDI JDBC datasource in Spring Boot
Solution 1
@Bean
public DataSource dataSource() {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
DataSource dataSource = dataSourceLookup.getDataSource("jdbc/apolloJNDI");
return dataSource;
}
No "java:comp/env/" its needed because JndiDataSourceLookup internaly calls convertJndiName that add this part. In other clases you should set the complete path.
Solution 2
If you're using Spring Boot 1.2 or greater, this got easier. You can just add this to application.properties
spring.datasource.jndi-name=java:comp/env/jdbc/my_database
The relevant docs: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connecting-to-a-jndi-datasource
Solution 3
Here's what I had done.
Add the following to to Application.java
@Bean
public DataSource dataSource() {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
DataSource dataSource = dataSourceLookup.getDataSource("java:comp/env/jdbc/mysqldb");
return dataSource;
}
Then follow the example in https://spring.io/guides/gs/accessing-data-jpa/ to set up the TransactionManager
and Hibernate specific properties.
Related videos on Youtube
Zac Tolley
Updated on July 27, 2022Comments
-
Zac Tolley over 1 year
I have a Spring boot application and want to deploy as a WAR to Tomcat 7. As part of this I need to keep configuration out of the WAR, so that I can deploy the same war to my stage and production servers and have it pickup the mysql connection via configuration.
To this end I want to configure my Spring Boot app to use a mysql connection configured as a JNDI datasource in the Tomcat instance.
Can spring boot do this and if so how?
Alternatively is this easy to do in Spring 4 without resorting to xml based configuration.
-
Dave Syer about 10 yearsIf you're using
@EnableAutoConfiguration
you a shouldn't need to set up a transaction manager (or any hibernate stuff for basic usage). -
Yue Liu about 10 yearsWhen deploying to tomcat, I was getting "'hibernate.dialect' not set". Is there a way to set that without creating a
HibernateJpaVendorAdapter
andsetDatabase(Database.ORACLE)
with@EnableAutoConfiguration
? -
Dave Syer about 10 yearsTry "spring.jpa.hibernate.dialect" (or "spring.jpa.hibernate." In general for hibernate native features, as opposed to Spring ones).
-
Dave Syer about 10 yearsActually, it's "spring.jpa.properties.hibernate.dialect" (see here), and spring.jpa.properties.hibernate.*" ("spring.jpa.hibernate.*" is used for some specific properties that are most commonly used).
-
Yue Liu about 10 yearsThanks! Guess I should have read the source for
HibernateJpaAutoConfiguration
too. -
cbmeeks about 6 yearsThis used to work for me but now I'm getting a
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
-
J. Abel almost 6 yearscbmeeks, Could you solve the exception?. I have the same problem.
-
J. Abel almost 6 yearsdustin.schultz, Do you know if Spring Boot works with jndl which are not in localhost. Something like this: spring.datasource.jndi-name=java:comp/env/192.151.101.179:7002/jdbc/Procesos
-
Prabhat Gaur about 3 yearsThe InitialContext error is really irritating as I removed it using datasource lookup in my configuration but still the integration test fails by checking application.yml when i intend it to use application-test.yml