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

Roman.77 :

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?

RamPrakash :

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");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=359553&siteId=1