I am trying to migrate my small application from Tomcat to WebSphere. In order to do so, I am rebuilding it from scratch addressing major components separately. I am struggling with data access / JNDI on webSphere Liberty. I get
javax.naming.NameNotFoundException: javax.naming.NameNotFoundException: java:comp/env/jdbc/test
SERVER.xml
<featureManager>
<feature>webProfile-8.0</feature>
<feature>localConnector-1.0</feature>
<feature>adminCenter-1.0</feature>
<feature>javaee-8.0</feature>
<feature>jndi-1.0</feature>
<feature>concurrent-1.0</feature>
<dataSource id="test" jndiName="jdbc/test" type="javax.sql.DataSource">
<jdbcDriver libraryRef="MySQLLib" />
<properties databaseName="test" serverName="localhost" portNumber="3306" user="user" password="mypassword" />
<jdbcDriver>
<library id="MySQLLib">
<fileset dir="/Library/JDBC/" includes="mysql-connector-java-5.1.14-bin.jar" />
</library>
</jdbcDriver>
</dataSource>
Data config class
@Configuration
public class DataSourceConfig {
@Resource(lookup = "java:comp/env/jdbc/test", name="java:comp/env/jdbc/test")
private DataSource dataSource;
I have also tried this approach :
@Bean
public DataSource dataSource() throws NamingException {
return (DataSource) new JndiTemplate().lookup("java:comp/env/jdbc/test");
}
DataSourceConfig
class. Just specifyspring.datasource,jndi-name
in your configuration and Spring Boot will do the lookup. – Social