20

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.

Zac Tolley
  • 2,340
  • 4
  • 19
  • 22

4 Answers4

16

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

dustin.schultz
  • 13,076
  • 7
  • 54
  • 63
  • 2
    This 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` – cbmeeks Mar 22 '18 at 16:23
  • cbmeeks, Could you solve the exception?. I have the same problem. – J. Abel May 16 '18 at 18:09
  • dustin.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 – J. Abel May 16 '18 at 18:12
  • The 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 – Prabhat Gaur Feb 05 '21 at 12:57
6
@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.

akokskis
  • 1,486
  • 15
  • 32
nekperu15739
  • 3,311
  • 2
  • 26
  • 25
4

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.

Yue Liu
  • 41
  • 2
  • If you're using `@EnableAutoConfiguration` you a shouldn't need to set up a transaction manager (or any hibernate stuff for basic usage). – Dave Syer Mar 01 '14 at 09:23
  • When deploying to tomcat, I was getting "'hibernate.dialect' not set". Is there a way to set that without creating a `HibernateJpaVendorAdapter` and `setDatabase(Database.ORACLE)` with `@EnableAutoConfiguration`? – Yue Liu Mar 01 '14 at 16:28
  • Try "spring.jpa.hibernate.dialect" (or "spring.jpa.hibernate." In general for hibernate native features, as opposed to Spring ones). – Dave Syer Mar 01 '14 at 16:42
  • 1
    Actually, it's "spring.jpa.properties.hibernate.dialect" (see [here](https://github.com/spring-projects/spring-boot/blob/master/docs/howto.md#configure-jpa-properties)), and spring.jpa.properties.hibernate.*" ("spring.jpa.hibernate.*" is used for some specific properties that are most commonly used). – Dave Syer Mar 02 '14 at 09:30
  • Thanks! Guess I should have read the source for `HibernateJpaAutoConfiguration` too. – Yue Liu Mar 02 '14 at 17:42
0

A hint for all of you using Spring Boot with an external Tomcat. Please ensure your war doesn't contain any tomcat jars. Multiple versions of same jar will produce hidden ClassCastException manifested by javax.naming.NamingException.

T.Dabrowski
  • 165
  • 1
  • 5
  • 14