1

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?

swapper9
  • 33
  • 1
  • 8

1 Answers1

4

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");
RamPrakash
  • 1,687
  • 3
  • 20
  • 25
  • in that case i got an error: 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 – swapper9 Dec 12 '19 at 15:07
  • Read this link https://stackoverflow.com/questions/6387238/what-does-javax-naming-noinitialcontextexception-mean. As you are using Websphere, You can specify jndi for database through admin console. https://www.ibm.com/support/knowledgecenter/en/SSEQTP_9.0.5/com.ibm.websphere.base.doc/ae/tdat_ccrtpds.html – RamPrakash Dec 12 '19 at 15:12
  • In the past I referred to below to articles while doing a POC app http://naruraghavan.github.io/deploying-spring-boot-applications-in-ibm-websphere-application-server/ https://support.bp-3.com/hc/en-us/articles/210443048 – RamPrakash Dec 12 '19 at 15:15
  • done all JNDI config through WAS admin console, rebooted WAS and still got same error and datasource null. What can I do? – swapper9 Dec 13 '19 at 13:29
  • @Roman.77 did you tested jndi through admin console. make sure your jndi in admin console matches with jndi defined in your spring boot app. – RamPrakash Dec 13 '19 at 14:41
  • Now it works, after I used the same jndi name in WAS as jndi name in app. Thanks a lot! – swapper9 Dec 17 '19 at 13:42
  • @Roman.77 if you think this answer is solution to problem/question them please accept the answer. – RamPrakash Dec 17 '19 at 14:07