Java – Weblogic exception : javax. naming. NameNotFoundException : Unable to resolve ‘jdbc.payment’ . Resolved ‘jdbc’ ; Remaining name ‘payment’

Weblogic exception : javax. naming. NameNotFoundException : Unable to resolve ‘jdbc.payment’ . Resolved ‘jdbc’ ; Remaining name ‘payment’… here is a solution to the problem.

Weblogic exception : javax. naming. NameNotFoundException : Unable to resolve ‘jdbc.payment’ . Resolved ‘jdbc’ ; Remaining name ‘payment’

I get this exception when I look up a jndi data source from Weblogic in a Spring Boot application… Only after one successful deployment… I mean starting with the second deployment. If I restart the container, it only works for the first deployment.

Caused: javax.naming.NameNotFoundException: Unable to resolve “jdbc.payment”. parsing ‘jdbc’; The remaining name ‘Payment’

A data source with the same name and attached to the management server.

I use the docker image: store/oracle/weblogic:12.2.1.4-dev and the environment variable PRODUCTION_MODE=dev

Update: If I disconnect the data source from the server and then reconnect it and then start war, it runs successfully once again ò

Update: Switching to a local installation of WebLogic is no longer dockerized and the behavior still occurs

Solution

This is a spring issue… Nothing to do with weblogic.

When war is closed, Spring removes the data source from the server JNDI tree, but the data source still runs on the server.
The operation of recreating or even reattaching the data source to the destination server adds it again to the JNDI tree.

A workaround for this behavior is to prevent Spring from calling the data source bean’s destroy method

@Primary
@Bean(name = "dataSource",destroyMethod = "")
@Profile("weblogic")
public DataSource dataSourceWeblogic() throws NamingException {

JndiTemplate jndiTemplate = new JndiTemplate();
        InitialContext ctx = (InitialContext) jndiTemplate.getContext();
        return  (javax.sql.DataSource) ctx.lookup(jndi);
}

Related Problems and Solutions