ederribeiro ederribeiro - 5 months ago 31
Java Question

Unable to resolve datasource JNDI after web application shutdown/start in Weblogic 12c

I have a DataSource configured in a Weblogic 12c as shown on the image below:

Datasource

And I also have a web application on the same Weblogic server that uses this datasource:

Web Application

Whenever I start Weblogic from the ground up everything works fine. But, after it is up, if I try to shutdown the webapplication and then start it again, I have an "javax.naming.NameNotFoundException" as shown below:

enter image description here

This is the code I am using to get the datasource:

@Bean
public DataSource dataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
DataSource dataSource = dsLookup.getDataSource("jdbc/xdrstoredbds");
return dataSource;
}


What may I be missing?

Answer

Ends up that the cause of the problem was a Spring behavior I was unaware of. As mentioned in this other answer Weblogic datasource disappears from JNDI tree I had to add a destroyMethod="" to my bean definition. Without this it seems that Spring

"tries to determine what the destroy method is. This is apparently causing the datasource to be closed and the JNDI key to be removed from the tree. Changing it to "" forces it to not look for a destroyMethod."

My method end up looking similarly the one in the answer I mentioned:

@Bean(destroyMethod = "")
public DataSource dataSource() {
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);
    return dsLookup.getDataSource("jdbc/xdrstoredbds");
}