I am using jdk 11 with Eclipse 2021 and WAS Liberty 21.0.0.4. I also have OpenLiberty 21.0.0.4 and the same issue happens there.
I have ConnectionFactory code that looks like this
public class ConnectionFactory {
private static DataSource ds = null;
...
public static Connection getDb2Connection() {
Connection conn = null;
try {
Context initCtx = new InitialContext();
ds = (DataSource) initCtx.lookup("jdbc/db2DataSource"); //NamingException is thrown here
conn = ds.getConnection();
} catch (NamingException ex) {
System.out.println(ex);
}
return conn;
}
}
The exception I get is
javax.naming.NamingException: CWWKN0008E: An object could not be obtained for name jdbc/db2DataSource
I provided below my solution which is to not use Liberty variable server.config.dir
but rather hardcode the path as explained in my answer below. This is not perfect solution and I hope someone will be able to explain why resolving Liberty variable by use of ${server.config.dir}
does not work.
In your answer, you posted that the following works,
But this does not work,
This suggests that you do not have a
resources
folder in theserver.config.dir
, and should just be doing,