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.
I created this question in order to hopefully get an answer but I found solution which is not what I hoped but I have no better.
After spending quite some time, I found that liberty server provided varialbe
server.config.dir(see https://www.ibm.com/docs/en/was-liberty/core?topic=liberty-directory-locations-properties) is the cause of this error.Following is how my Liberty server.xml is set.
I dont put hardcoded values in server.xml but I put them in server.env file like so: The above variables are set hardcoded inside server.env like:
, and then in my server.xml, I reference these variables using ${var-name} notation like here to set my JNDI name for data source:
This is all fine, however in the above dataSource, I have
libraryRef="DB2Library"and my DB2Library is then set as:Notice the 2 commented out lines that use
${server.config.dir}. They are the same except one uses forward slash and the other backward slash. Both of them will cause this NamingException.The line with hardcoded path will work fine.
So, it seems like the Liberty resolution of variable
${server.config.dir}is not working properly.