JNDI SPRING BOOT connection to WebSphere Liberty server

I have an error when making the connection, I have the following settings.

application.properties (Definition of the name JNDI)

spring.datasource.jndi-name=java:comp/env/jdbc/conndb

ConexionController.java (Configuration JNDI)

@Configuration
public class ConexionController {

    @Bean
    public DataSource dataSource() throws NamingException {
        JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
        bean.setJndiName("java:comp/env/jdbc/conndb");
        bean.setProxyInterface(DataSource.class);
        bean.setLookupOnStartup(false);
        bean.afterPropertiesSet();
        return (DataSource) bean.getObject();
    }
}

TestController.java (Controller to check the connection)

@RestController
public class TestController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/testConnection")
    public String testConnection() {
        try {
            jdbcTemplate.execute("SELECT 1 FROM DUAL");
            return "Connection successful!";
        } catch (Exception e) {
            return "Connection failed: " + e.getMessage();
        }
    }
}

server.xml (Liberty server configuration)

<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>webProfile-8.0</feature>
        <feature>jdbc-4.0</feature>
        <feature>jndi-1.0</feature>
    </featureManager>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint id="defaultHttpEndpoint"
                  httpPort="9080"
                  httpsPort="9443" />
                  
    <!-- Automatically expand WAR files and EAR files -->
    <applicationManager autoExpand="true"/>

    <dataSource jndiName="jdbc/conndb" id="conndb"
            connectionManager="default">
            <jdbcDriver libraryRef="OracleLib"/>
            <properties.oracle URL="jdbc:oracle:thin:@10.142.108.10:7800/DATABASE" user="root" password="12345678"/>
    </dataSource>  
    
    <library id="OracleLib">
        <file dir="C:\Liberty\drivers\oracle\ojdbc8.jar" includes="ojdbc8.jar"/>
    </library>

</server>

The error that shows me when entering http://localhost:9080/conexionJndi-1/testConnection is:

Connection failed: JndiObjectTargetSource failed to obtain new target object; nested exception is javax.naming.NameNotFoundException: javax.naming.NameNotFoundException: java:comp/env/jdbc/conndb

I've tried a lot of things and it doesn't work, I need help.

Upvotes: 1

Views: 36

Answers (0)

Related Questions