2

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?

ederribeiro
  • 388
  • 1
  • 3
  • 15

2 Answers2

2

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");
} 
Community
  • 1
  • 1
ederribeiro
  • 388
  • 1
  • 3
  • 15
0

Replace your line

DataSource dataSource = dsLookup.getDataSource("jdbc/xdrstoredbds");

with

DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/xdrstoredbds"); 

Then try to run in again.
java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component

Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
  • Replace your line of code segment DataSource dataSource = dsLookup.getDataSource("jdbc/xdrstoredbds"); with below one DataSource dataSource = dsLookup.getDataSource("java:comp/env/jdbc/xdrstoredbds"); Then try to run in again. java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component – Sandip K Jun 16 '16 at 04:21
  • Unfortunately this did not work. I now get this exception: weblogic.application.ModuleException: javax.naming.NameNotFoundException: While trying to look up comp/env/jdbc/xdrstoredbds in /app/webapp/xdrweb.war/490669642.; remaining name 'comp/env/jdbc/xdrstoredbds'. This error happens when I start Weblogic or when I try to start only de webapplication. – ederribeiro Jun 16 '16 at 13:06