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

24,077

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.

Share:
24,077

Related videos on Youtube

Zac Tolley
Author by

Zac Tolley

Updated on July 27, 2022

Comments

  • Zac Tolley
    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
    Dave Syer about 10 years
    If you're using @EnableAutoConfiguration you a shouldn't need to set up a transaction manager (or any hibernate stuff for basic usage).
  • Yue Liu
    Yue Liu about 10 years
    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?
  • Dave Syer
    Dave Syer about 10 years
    Try "spring.jpa.hibernate.dialect" (or "spring.jpa.hibernate." In general for hibernate native features, as opposed to Spring ones).
  • Dave Syer
    Dave Syer about 10 years
    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).
  • Yue Liu
    Yue Liu about 10 years
    Thanks! Guess I should have read the source for HibernateJpaAutoConfiguration too.
  • cbmeeks
    cbmeeks about 6 years
    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
  • J. Abel
    J. Abel almost 6 years
    cbmeeks, Could you solve the exception?. I have the same problem.
  • J. Abel
    J. Abel almost 6 years
    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:70‌​02/jdbc/Procesos
  • Prabhat Gaur
    Prabhat Gaur about 3 years
    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

Related