How do you use a Tomcat JNDI JDBC datasource in Spring Boot
Asked Answered
D

4

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.

Dicephalous answered 28/2, 2014 at 17:7 Comment(1)
guessing: can't you use the org.springframework.jndi.JndiObjectFactoryBeanMelancholia
S
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.

Segmental answered 12/9, 2014 at 12:14 Comment(0)
O
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

Obsession answered 20/3, 2015 at 20:24 Comment(4)
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.initialMufinella
cbmeeks, Could you solve the exception?. I have the same problem.Acetone
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/ProcesosAcetone
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.ymlEpimenides
S
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.

Segmental answered 12/9, 2014 at 12:14 Comment(0)
C
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.

Cancroid answered 28/2, 2014 at 19:41 Comment(5)
If you're using @EnableAutoConfiguration you a shouldn't need to set up a transaction manager (or any hibernate stuff for basic usage).Cruise
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?Cancroid
Try "spring.jpa.hibernate.dialect" (or "spring.jpa.hibernate." In general for hibernate native features, as opposed to Spring ones).Cruise
Actually, 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).Cruise
Thanks! Guess I should have read the source for HibernateJpaAutoConfiguration too.Cancroid
C
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.

Cotquean answered 26/11, 2020 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.