Search code examples
javaspring-bootwebspherejndi

How to get JNDI datasource in Spring Boot from IBM WebSphere 9


Trying to deploy SpringBoot app to WebSphere 9 with using jdbc as SQL Server. I need to get jdbc username and password from websphere settings (not from app.properties).

As I understand, I can use InitialConfig.lookup() or JndiDataSourceLookup, but how could I find database, username and password?


Solution

  • Option 1:

    Add the following property into application.properties

    spring.datasource.jndi-name=jdbc/jndiName
    

    add datasource bean definition in spring boot main class or config class

     @Autowired
     private Environment env;
    
    @Bean
    public DataSource dataSource() throws NamingException {
        return (DataSource) new JndiTemplate()
                                 .lookup(env.getProperty("spring.datasource.jndi-name"));
     }
    

    Option 2:

    using Java standard API

    DataSource ds = javax.naming.InitialContext.doLookup("jdbc/jndiName");